how to upload image in the storage directory in php it gives me error “image is not uploaded”












-1















Getting error while uploading the image. error is image in not uploaded kindly help me to figure this our.



class User extends DatabaseConnection{

public function addNewUser() {
$db = $this->getConn();

$add_query = "INSERT into `users`"
. "(`user_id` , `first_name` , `middle_name` , `last_name` , `user_name` , `email` , `contact_number` , `password` , `gender` , `date_of_birth` , `country` , `city` , `profile_image` , `created_at` , `updated_at` , `role`)"
. "VALUES"
. "('NULL' , '$this->first_name' , '$this->middle_name' , '$this->last_name' , '$this->user_name' , '$this->email' , '$this->contact_number' , '$this->password' , '$this->gender' , '$this->dob' , '$this->country' , '$this->city' , '$this->profile_image' , NOW() , NOW() , 'visitor')";

$db->query($add_query);
if ($db->errno) {
throw new Exception($db->error);


$storage_path = "../storage/" . "$this->user_name " . "/";

if (!is_dir('../storage')) {
if (!mkdir('../storage')) {
throw new Exception("Storage Directory not created");
}
}

if (!is_dir("../storage/$this->user_name")) {
if (!mkdir("../storage/$this->user_name")) {
throw new Exception("User Directory not created");
}
}

$upload = move_uploaded_file($this->profile_tmp_name, $storage_path . $this->profile_image);

if (!$upload) {
throw new Exception("User Image not uploaded");
}
}
}
}









share|improve this question

























  • You should echo your $storage_path. I don't think it will look like you're expecting. Also, check your servers error log for a proper error message:

    – Magnus Eriksson
    Nov 19 '18 at 6:48













  • Which line does that error message come from? Because it's not in the code you added

    – Akintunde-Rotimi
    Nov 19 '18 at 6:50






  • 2





    You will need to start basic debugging. Log variable contents into a log file or, better, use a server side debugger to step through your code. There are many tutorials available that explain how to get started with that.

    – arkascha
    Nov 19 '18 at 6:50













  • . "$this->user_name " . contains a trailing space that I think is unintentional. You could change that to: . $this->user_name .. No need for the quotes there.

    – Magnus Eriksson
    Nov 19 '18 at 7:16













  • actually the problem is .. it make the storage path in mean storage directory but the image is not getting save in the storage thats the mean problem have a look on the check $upload = move_uploaded_file($this->profile_tmp_name, $storage_path . $this->profile_image); if (!$upload) { throw new Exception("User Image not uploaded"); } this is the prob the errir us "user image not uploaded"

    – Mohammad nisar
    Nov 19 '18 at 13:53


















-1















Getting error while uploading the image. error is image in not uploaded kindly help me to figure this our.



class User extends DatabaseConnection{

public function addNewUser() {
$db = $this->getConn();

$add_query = "INSERT into `users`"
. "(`user_id` , `first_name` , `middle_name` , `last_name` , `user_name` , `email` , `contact_number` , `password` , `gender` , `date_of_birth` , `country` , `city` , `profile_image` , `created_at` , `updated_at` , `role`)"
. "VALUES"
. "('NULL' , '$this->first_name' , '$this->middle_name' , '$this->last_name' , '$this->user_name' , '$this->email' , '$this->contact_number' , '$this->password' , '$this->gender' , '$this->dob' , '$this->country' , '$this->city' , '$this->profile_image' , NOW() , NOW() , 'visitor')";

$db->query($add_query);
if ($db->errno) {
throw new Exception($db->error);


$storage_path = "../storage/" . "$this->user_name " . "/";

if (!is_dir('../storage')) {
if (!mkdir('../storage')) {
throw new Exception("Storage Directory not created");
}
}

if (!is_dir("../storage/$this->user_name")) {
if (!mkdir("../storage/$this->user_name")) {
throw new Exception("User Directory not created");
}
}

$upload = move_uploaded_file($this->profile_tmp_name, $storage_path . $this->profile_image);

if (!$upload) {
throw new Exception("User Image not uploaded");
}
}
}
}









share|improve this question

























  • You should echo your $storage_path. I don't think it will look like you're expecting. Also, check your servers error log for a proper error message:

    – Magnus Eriksson
    Nov 19 '18 at 6:48













  • Which line does that error message come from? Because it's not in the code you added

    – Akintunde-Rotimi
    Nov 19 '18 at 6:50






  • 2





    You will need to start basic debugging. Log variable contents into a log file or, better, use a server side debugger to step through your code. There are many tutorials available that explain how to get started with that.

    – arkascha
    Nov 19 '18 at 6:50













  • . "$this->user_name " . contains a trailing space that I think is unintentional. You could change that to: . $this->user_name .. No need for the quotes there.

    – Magnus Eriksson
    Nov 19 '18 at 7:16













  • actually the problem is .. it make the storage path in mean storage directory but the image is not getting save in the storage thats the mean problem have a look on the check $upload = move_uploaded_file($this->profile_tmp_name, $storage_path . $this->profile_image); if (!$upload) { throw new Exception("User Image not uploaded"); } this is the prob the errir us "user image not uploaded"

    – Mohammad nisar
    Nov 19 '18 at 13:53
















-1












-1








-1








Getting error while uploading the image. error is image in not uploaded kindly help me to figure this our.



class User extends DatabaseConnection{

public function addNewUser() {
$db = $this->getConn();

$add_query = "INSERT into `users`"
. "(`user_id` , `first_name` , `middle_name` , `last_name` , `user_name` , `email` , `contact_number` , `password` , `gender` , `date_of_birth` , `country` , `city` , `profile_image` , `created_at` , `updated_at` , `role`)"
. "VALUES"
. "('NULL' , '$this->first_name' , '$this->middle_name' , '$this->last_name' , '$this->user_name' , '$this->email' , '$this->contact_number' , '$this->password' , '$this->gender' , '$this->dob' , '$this->country' , '$this->city' , '$this->profile_image' , NOW() , NOW() , 'visitor')";

$db->query($add_query);
if ($db->errno) {
throw new Exception($db->error);


$storage_path = "../storage/" . "$this->user_name " . "/";

if (!is_dir('../storage')) {
if (!mkdir('../storage')) {
throw new Exception("Storage Directory not created");
}
}

if (!is_dir("../storage/$this->user_name")) {
if (!mkdir("../storage/$this->user_name")) {
throw new Exception("User Directory not created");
}
}

$upload = move_uploaded_file($this->profile_tmp_name, $storage_path . $this->profile_image);

if (!$upload) {
throw new Exception("User Image not uploaded");
}
}
}
}









share|improve this question
















Getting error while uploading the image. error is image in not uploaded kindly help me to figure this our.



class User extends DatabaseConnection{

public function addNewUser() {
$db = $this->getConn();

$add_query = "INSERT into `users`"
. "(`user_id` , `first_name` , `middle_name` , `last_name` , `user_name` , `email` , `contact_number` , `password` , `gender` , `date_of_birth` , `country` , `city` , `profile_image` , `created_at` , `updated_at` , `role`)"
. "VALUES"
. "('NULL' , '$this->first_name' , '$this->middle_name' , '$this->last_name' , '$this->user_name' , '$this->email' , '$this->contact_number' , '$this->password' , '$this->gender' , '$this->dob' , '$this->country' , '$this->city' , '$this->profile_image' , NOW() , NOW() , 'visitor')";

$db->query($add_query);
if ($db->errno) {
throw new Exception($db->error);


$storage_path = "../storage/" . "$this->user_name " . "/";

if (!is_dir('../storage')) {
if (!mkdir('../storage')) {
throw new Exception("Storage Directory not created");
}
}

if (!is_dir("../storage/$this->user_name")) {
if (!mkdir("../storage/$this->user_name")) {
throw new Exception("User Directory not created");
}
}

$upload = move_uploaded_file($this->profile_tmp_name, $storage_path . $this->profile_image);

if (!$upload) {
throw new Exception("User Image not uploaded");
}
}
}
}






php image oop upload






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 6:48









Hossein Golshani

2,05851023




2,05851023










asked Nov 19 '18 at 6:44









Mohammad nisarMohammad nisar

41




41













  • You should echo your $storage_path. I don't think it will look like you're expecting. Also, check your servers error log for a proper error message:

    – Magnus Eriksson
    Nov 19 '18 at 6:48













  • Which line does that error message come from? Because it's not in the code you added

    – Akintunde-Rotimi
    Nov 19 '18 at 6:50






  • 2





    You will need to start basic debugging. Log variable contents into a log file or, better, use a server side debugger to step through your code. There are many tutorials available that explain how to get started with that.

    – arkascha
    Nov 19 '18 at 6:50













  • . "$this->user_name " . contains a trailing space that I think is unintentional. You could change that to: . $this->user_name .. No need for the quotes there.

    – Magnus Eriksson
    Nov 19 '18 at 7:16













  • actually the problem is .. it make the storage path in mean storage directory but the image is not getting save in the storage thats the mean problem have a look on the check $upload = move_uploaded_file($this->profile_tmp_name, $storage_path . $this->profile_image); if (!$upload) { throw new Exception("User Image not uploaded"); } this is the prob the errir us "user image not uploaded"

    – Mohammad nisar
    Nov 19 '18 at 13:53





















  • You should echo your $storage_path. I don't think it will look like you're expecting. Also, check your servers error log for a proper error message:

    – Magnus Eriksson
    Nov 19 '18 at 6:48













  • Which line does that error message come from? Because it's not in the code you added

    – Akintunde-Rotimi
    Nov 19 '18 at 6:50






  • 2





    You will need to start basic debugging. Log variable contents into a log file or, better, use a server side debugger to step through your code. There are many tutorials available that explain how to get started with that.

    – arkascha
    Nov 19 '18 at 6:50













  • . "$this->user_name " . contains a trailing space that I think is unintentional. You could change that to: . $this->user_name .. No need for the quotes there.

    – Magnus Eriksson
    Nov 19 '18 at 7:16













  • actually the problem is .. it make the storage path in mean storage directory but the image is not getting save in the storage thats the mean problem have a look on the check $upload = move_uploaded_file($this->profile_tmp_name, $storage_path . $this->profile_image); if (!$upload) { throw new Exception("User Image not uploaded"); } this is the prob the errir us "user image not uploaded"

    – Mohammad nisar
    Nov 19 '18 at 13:53



















You should echo your $storage_path. I don't think it will look like you're expecting. Also, check your servers error log for a proper error message:

– Magnus Eriksson
Nov 19 '18 at 6:48







You should echo your $storage_path. I don't think it will look like you're expecting. Also, check your servers error log for a proper error message:

– Magnus Eriksson
Nov 19 '18 at 6:48















Which line does that error message come from? Because it's not in the code you added

– Akintunde-Rotimi
Nov 19 '18 at 6:50





Which line does that error message come from? Because it's not in the code you added

– Akintunde-Rotimi
Nov 19 '18 at 6:50




2




2





You will need to start basic debugging. Log variable contents into a log file or, better, use a server side debugger to step through your code. There are many tutorials available that explain how to get started with that.

– arkascha
Nov 19 '18 at 6:50







You will need to start basic debugging. Log variable contents into a log file or, better, use a server side debugger to step through your code. There are many tutorials available that explain how to get started with that.

– arkascha
Nov 19 '18 at 6:50















. "$this->user_name " . contains a trailing space that I think is unintentional. You could change that to: . $this->user_name .. No need for the quotes there.

– Magnus Eriksson
Nov 19 '18 at 7:16







. "$this->user_name " . contains a trailing space that I think is unintentional. You could change that to: . $this->user_name .. No need for the quotes there.

– Magnus Eriksson
Nov 19 '18 at 7:16















actually the problem is .. it make the storage path in mean storage directory but the image is not getting save in the storage thats the mean problem have a look on the check $upload = move_uploaded_file($this->profile_tmp_name, $storage_path . $this->profile_image); if (!$upload) { throw new Exception("User Image not uploaded"); } this is the prob the errir us "user image not uploaded"

– Mohammad nisar
Nov 19 '18 at 13:53







actually the problem is .. it make the storage path in mean storage directory but the image is not getting save in the storage thats the mean problem have a look on the check $upload = move_uploaded_file($this->profile_tmp_name, $storage_path . $this->profile_image); if (!$upload) { throw new Exception("User Image not uploaded"); } this is the prob the errir us "user image not uploaded"

– Mohammad nisar
Nov 19 '18 at 13:53














1 Answer
1






active

oldest

votes


















-2














I think that the main problem here lies in the type of strings that you have used.



In PHP there are at least 2 string ways in which strings can be represented.




  • single quotes -> writes everything as it is, e.g 'hello $world' will literally print hello $world.


  • double quotes -> prints out the variables that are embedded within, e.g "$data" will print out the value of the variable $data. Note data variables which are not "simple", or rather those which have qualifiers such as $this->data or even $row['data'] have to enclosed inbetween the curly braces like so "{$this->data}" and "{$row['data']}" or else you will make it difficult for the parser to parse them, and hence you might get unexpected results.



Having said that, your code can be changed to the following...



$add_query = "INSERT into `users`"
. "(`user_id` , `first_name` , `middle_name` , `last_name` , `user_name` , `email` , `contact_number` , `password` , `gender` , `date_of_birth` , `country` , `city` , `profile_image` , `created_at` , `updated_at` , `role`)"
. "VALUES"
. "('NULL' , {$this->first_name} , {$this->middle_name} , {$this->last_name} , {$this->user_name} , {$this->email} , {$this->contact_number} , {$this->password} , {$this->gender} , {$this->dob} , {$this->country} , {$this->city} , {$this->profile_image} , NOW() , NOW() , 'visitor')";

$db->query($add_query);
if ($db->errno) {
throw new Exception($db->error);


$storage_path = "../storage/" . $this->user_name . "/";

if (!is_dir('../storage')) {
if (!mkdir('../storage')) {
throw new Exception("Storage Directory not created");
}
}

if (!is_dir("../storage/{$this->user_name}")) {
if (!mkdir("../storage/{$this->user_name}")) {
throw new Exception("User Directory not created");
}
}


$upload = move_uploaded_file($this->profile_tmp_name, $storage_path . $this->profile_image);

if (!$upload) {
throw new Exception("User Image not uploaded");
}
}


However if the problems persists. Then you have to make sure that the directory in which you are trying to move the uploaded file already exists and has write permissions for the user that is trying to write to it. usually on UNIX this can be achieved by chmod 777 /path/to/directory.






share|improve this answer


























  • If you look at the query the OP have written, it is all inside double quotes so your change will actually produce syntax errors. More than that, you've put the quotes for $this->last_name in the wrong place. Your new double quotes would end the string. The query isn't the OP's problem. Or at least not the problem the question refers to.

    – Magnus Eriksson
    Nov 19 '18 at 7:13













  • @MagnusEriksson clearly that was a mistake. The least you could have done, was to edit it

    – marvinIsSacul
    Nov 19 '18 at 7:19






  • 1





    Regardless of the last_name typo, that query will still produce syntax errors. Like I mentioned, the query is already inside double quotes so the OP's original version would actually work as expected. Yours won't. Here's a demo

    – Magnus Eriksson
    Nov 19 '18 at 7:20













  • And I do agree with that. All I was suggesting was that you edit

    – marvinIsSacul
    Nov 19 '18 at 7:24






  • 1





    That would more or less be editing your answer to look like the OP's code. What would be the point of that? Before the OP has done some debugging, any answer would just be stabs in the dark. Let's wait until the OP has updated the question with all the relevant information.

    – Magnus Eriksson
    Nov 19 '18 at 9:37











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53369556%2fhow-to-upload-image-in-the-storage-directory-in-php-it-gives-me-error-image-is%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-2














I think that the main problem here lies in the type of strings that you have used.



In PHP there are at least 2 string ways in which strings can be represented.




  • single quotes -> writes everything as it is, e.g 'hello $world' will literally print hello $world.


  • double quotes -> prints out the variables that are embedded within, e.g "$data" will print out the value of the variable $data. Note data variables which are not "simple", or rather those which have qualifiers such as $this->data or even $row['data'] have to enclosed inbetween the curly braces like so "{$this->data}" and "{$row['data']}" or else you will make it difficult for the parser to parse them, and hence you might get unexpected results.



Having said that, your code can be changed to the following...



$add_query = "INSERT into `users`"
. "(`user_id` , `first_name` , `middle_name` , `last_name` , `user_name` , `email` , `contact_number` , `password` , `gender` , `date_of_birth` , `country` , `city` , `profile_image` , `created_at` , `updated_at` , `role`)"
. "VALUES"
. "('NULL' , {$this->first_name} , {$this->middle_name} , {$this->last_name} , {$this->user_name} , {$this->email} , {$this->contact_number} , {$this->password} , {$this->gender} , {$this->dob} , {$this->country} , {$this->city} , {$this->profile_image} , NOW() , NOW() , 'visitor')";

$db->query($add_query);
if ($db->errno) {
throw new Exception($db->error);


$storage_path = "../storage/" . $this->user_name . "/";

if (!is_dir('../storage')) {
if (!mkdir('../storage')) {
throw new Exception("Storage Directory not created");
}
}

if (!is_dir("../storage/{$this->user_name}")) {
if (!mkdir("../storage/{$this->user_name}")) {
throw new Exception("User Directory not created");
}
}


$upload = move_uploaded_file($this->profile_tmp_name, $storage_path . $this->profile_image);

if (!$upload) {
throw new Exception("User Image not uploaded");
}
}


However if the problems persists. Then you have to make sure that the directory in which you are trying to move the uploaded file already exists and has write permissions for the user that is trying to write to it. usually on UNIX this can be achieved by chmod 777 /path/to/directory.






share|improve this answer


























  • If you look at the query the OP have written, it is all inside double quotes so your change will actually produce syntax errors. More than that, you've put the quotes for $this->last_name in the wrong place. Your new double quotes would end the string. The query isn't the OP's problem. Or at least not the problem the question refers to.

    – Magnus Eriksson
    Nov 19 '18 at 7:13













  • @MagnusEriksson clearly that was a mistake. The least you could have done, was to edit it

    – marvinIsSacul
    Nov 19 '18 at 7:19






  • 1





    Regardless of the last_name typo, that query will still produce syntax errors. Like I mentioned, the query is already inside double quotes so the OP's original version would actually work as expected. Yours won't. Here's a demo

    – Magnus Eriksson
    Nov 19 '18 at 7:20













  • And I do agree with that. All I was suggesting was that you edit

    – marvinIsSacul
    Nov 19 '18 at 7:24






  • 1





    That would more or less be editing your answer to look like the OP's code. What would be the point of that? Before the OP has done some debugging, any answer would just be stabs in the dark. Let's wait until the OP has updated the question with all the relevant information.

    – Magnus Eriksson
    Nov 19 '18 at 9:37
















-2














I think that the main problem here lies in the type of strings that you have used.



In PHP there are at least 2 string ways in which strings can be represented.




  • single quotes -> writes everything as it is, e.g 'hello $world' will literally print hello $world.


  • double quotes -> prints out the variables that are embedded within, e.g "$data" will print out the value of the variable $data. Note data variables which are not "simple", or rather those which have qualifiers such as $this->data or even $row['data'] have to enclosed inbetween the curly braces like so "{$this->data}" and "{$row['data']}" or else you will make it difficult for the parser to parse them, and hence you might get unexpected results.



Having said that, your code can be changed to the following...



$add_query = "INSERT into `users`"
. "(`user_id` , `first_name` , `middle_name` , `last_name` , `user_name` , `email` , `contact_number` , `password` , `gender` , `date_of_birth` , `country` , `city` , `profile_image` , `created_at` , `updated_at` , `role`)"
. "VALUES"
. "('NULL' , {$this->first_name} , {$this->middle_name} , {$this->last_name} , {$this->user_name} , {$this->email} , {$this->contact_number} , {$this->password} , {$this->gender} , {$this->dob} , {$this->country} , {$this->city} , {$this->profile_image} , NOW() , NOW() , 'visitor')";

$db->query($add_query);
if ($db->errno) {
throw new Exception($db->error);


$storage_path = "../storage/" . $this->user_name . "/";

if (!is_dir('../storage')) {
if (!mkdir('../storage')) {
throw new Exception("Storage Directory not created");
}
}

if (!is_dir("../storage/{$this->user_name}")) {
if (!mkdir("../storage/{$this->user_name}")) {
throw new Exception("User Directory not created");
}
}


$upload = move_uploaded_file($this->profile_tmp_name, $storage_path . $this->profile_image);

if (!$upload) {
throw new Exception("User Image not uploaded");
}
}


However if the problems persists. Then you have to make sure that the directory in which you are trying to move the uploaded file already exists and has write permissions for the user that is trying to write to it. usually on UNIX this can be achieved by chmod 777 /path/to/directory.






share|improve this answer


























  • If you look at the query the OP have written, it is all inside double quotes so your change will actually produce syntax errors. More than that, you've put the quotes for $this->last_name in the wrong place. Your new double quotes would end the string. The query isn't the OP's problem. Or at least not the problem the question refers to.

    – Magnus Eriksson
    Nov 19 '18 at 7:13













  • @MagnusEriksson clearly that was a mistake. The least you could have done, was to edit it

    – marvinIsSacul
    Nov 19 '18 at 7:19






  • 1





    Regardless of the last_name typo, that query will still produce syntax errors. Like I mentioned, the query is already inside double quotes so the OP's original version would actually work as expected. Yours won't. Here's a demo

    – Magnus Eriksson
    Nov 19 '18 at 7:20













  • And I do agree with that. All I was suggesting was that you edit

    – marvinIsSacul
    Nov 19 '18 at 7:24






  • 1





    That would more or less be editing your answer to look like the OP's code. What would be the point of that? Before the OP has done some debugging, any answer would just be stabs in the dark. Let's wait until the OP has updated the question with all the relevant information.

    – Magnus Eriksson
    Nov 19 '18 at 9:37














-2












-2








-2







I think that the main problem here lies in the type of strings that you have used.



In PHP there are at least 2 string ways in which strings can be represented.




  • single quotes -> writes everything as it is, e.g 'hello $world' will literally print hello $world.


  • double quotes -> prints out the variables that are embedded within, e.g "$data" will print out the value of the variable $data. Note data variables which are not "simple", or rather those which have qualifiers such as $this->data or even $row['data'] have to enclosed inbetween the curly braces like so "{$this->data}" and "{$row['data']}" or else you will make it difficult for the parser to parse them, and hence you might get unexpected results.



Having said that, your code can be changed to the following...



$add_query = "INSERT into `users`"
. "(`user_id` , `first_name` , `middle_name` , `last_name` , `user_name` , `email` , `contact_number` , `password` , `gender` , `date_of_birth` , `country` , `city` , `profile_image` , `created_at` , `updated_at` , `role`)"
. "VALUES"
. "('NULL' , {$this->first_name} , {$this->middle_name} , {$this->last_name} , {$this->user_name} , {$this->email} , {$this->contact_number} , {$this->password} , {$this->gender} , {$this->dob} , {$this->country} , {$this->city} , {$this->profile_image} , NOW() , NOW() , 'visitor')";

$db->query($add_query);
if ($db->errno) {
throw new Exception($db->error);


$storage_path = "../storage/" . $this->user_name . "/";

if (!is_dir('../storage')) {
if (!mkdir('../storage')) {
throw new Exception("Storage Directory not created");
}
}

if (!is_dir("../storage/{$this->user_name}")) {
if (!mkdir("../storage/{$this->user_name}")) {
throw new Exception("User Directory not created");
}
}


$upload = move_uploaded_file($this->profile_tmp_name, $storage_path . $this->profile_image);

if (!$upload) {
throw new Exception("User Image not uploaded");
}
}


However if the problems persists. Then you have to make sure that the directory in which you are trying to move the uploaded file already exists and has write permissions for the user that is trying to write to it. usually on UNIX this can be achieved by chmod 777 /path/to/directory.






share|improve this answer















I think that the main problem here lies in the type of strings that you have used.



In PHP there are at least 2 string ways in which strings can be represented.




  • single quotes -> writes everything as it is, e.g 'hello $world' will literally print hello $world.


  • double quotes -> prints out the variables that are embedded within, e.g "$data" will print out the value of the variable $data. Note data variables which are not "simple", or rather those which have qualifiers such as $this->data or even $row['data'] have to enclosed inbetween the curly braces like so "{$this->data}" and "{$row['data']}" or else you will make it difficult for the parser to parse them, and hence you might get unexpected results.



Having said that, your code can be changed to the following...



$add_query = "INSERT into `users`"
. "(`user_id` , `first_name` , `middle_name` , `last_name` , `user_name` , `email` , `contact_number` , `password` , `gender` , `date_of_birth` , `country` , `city` , `profile_image` , `created_at` , `updated_at` , `role`)"
. "VALUES"
. "('NULL' , {$this->first_name} , {$this->middle_name} , {$this->last_name} , {$this->user_name} , {$this->email} , {$this->contact_number} , {$this->password} , {$this->gender} , {$this->dob} , {$this->country} , {$this->city} , {$this->profile_image} , NOW() , NOW() , 'visitor')";

$db->query($add_query);
if ($db->errno) {
throw new Exception($db->error);


$storage_path = "../storage/" . $this->user_name . "/";

if (!is_dir('../storage')) {
if (!mkdir('../storage')) {
throw new Exception("Storage Directory not created");
}
}

if (!is_dir("../storage/{$this->user_name}")) {
if (!mkdir("../storage/{$this->user_name}")) {
throw new Exception("User Directory not created");
}
}


$upload = move_uploaded_file($this->profile_tmp_name, $storage_path . $this->profile_image);

if (!$upload) {
throw new Exception("User Image not uploaded");
}
}


However if the problems persists. Then you have to make sure that the directory in which you are trying to move the uploaded file already exists and has write permissions for the user that is trying to write to it. usually on UNIX this can be achieved by chmod 777 /path/to/directory.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 7:20

























answered Nov 19 '18 at 7:02









marvinIsSaculmarvinIsSacul

53917




53917













  • If you look at the query the OP have written, it is all inside double quotes so your change will actually produce syntax errors. More than that, you've put the quotes for $this->last_name in the wrong place. Your new double quotes would end the string. The query isn't the OP's problem. Or at least not the problem the question refers to.

    – Magnus Eriksson
    Nov 19 '18 at 7:13













  • @MagnusEriksson clearly that was a mistake. The least you could have done, was to edit it

    – marvinIsSacul
    Nov 19 '18 at 7:19






  • 1





    Regardless of the last_name typo, that query will still produce syntax errors. Like I mentioned, the query is already inside double quotes so the OP's original version would actually work as expected. Yours won't. Here's a demo

    – Magnus Eriksson
    Nov 19 '18 at 7:20













  • And I do agree with that. All I was suggesting was that you edit

    – marvinIsSacul
    Nov 19 '18 at 7:24






  • 1





    That would more or less be editing your answer to look like the OP's code. What would be the point of that? Before the OP has done some debugging, any answer would just be stabs in the dark. Let's wait until the OP has updated the question with all the relevant information.

    – Magnus Eriksson
    Nov 19 '18 at 9:37



















  • If you look at the query the OP have written, it is all inside double quotes so your change will actually produce syntax errors. More than that, you've put the quotes for $this->last_name in the wrong place. Your new double quotes would end the string. The query isn't the OP's problem. Or at least not the problem the question refers to.

    – Magnus Eriksson
    Nov 19 '18 at 7:13













  • @MagnusEriksson clearly that was a mistake. The least you could have done, was to edit it

    – marvinIsSacul
    Nov 19 '18 at 7:19






  • 1





    Regardless of the last_name typo, that query will still produce syntax errors. Like I mentioned, the query is already inside double quotes so the OP's original version would actually work as expected. Yours won't. Here's a demo

    – Magnus Eriksson
    Nov 19 '18 at 7:20













  • And I do agree with that. All I was suggesting was that you edit

    – marvinIsSacul
    Nov 19 '18 at 7:24






  • 1





    That would more or less be editing your answer to look like the OP's code. What would be the point of that? Before the OP has done some debugging, any answer would just be stabs in the dark. Let's wait until the OP has updated the question with all the relevant information.

    – Magnus Eriksson
    Nov 19 '18 at 9:37

















If you look at the query the OP have written, it is all inside double quotes so your change will actually produce syntax errors. More than that, you've put the quotes for $this->last_name in the wrong place. Your new double quotes would end the string. The query isn't the OP's problem. Or at least not the problem the question refers to.

– Magnus Eriksson
Nov 19 '18 at 7:13







If you look at the query the OP have written, it is all inside double quotes so your change will actually produce syntax errors. More than that, you've put the quotes for $this->last_name in the wrong place. Your new double quotes would end the string. The query isn't the OP's problem. Or at least not the problem the question refers to.

– Magnus Eriksson
Nov 19 '18 at 7:13















@MagnusEriksson clearly that was a mistake. The least you could have done, was to edit it

– marvinIsSacul
Nov 19 '18 at 7:19





@MagnusEriksson clearly that was a mistake. The least you could have done, was to edit it

– marvinIsSacul
Nov 19 '18 at 7:19




1




1





Regardless of the last_name typo, that query will still produce syntax errors. Like I mentioned, the query is already inside double quotes so the OP's original version would actually work as expected. Yours won't. Here's a demo

– Magnus Eriksson
Nov 19 '18 at 7:20







Regardless of the last_name typo, that query will still produce syntax errors. Like I mentioned, the query is already inside double quotes so the OP's original version would actually work as expected. Yours won't. Here's a demo

– Magnus Eriksson
Nov 19 '18 at 7:20















And I do agree with that. All I was suggesting was that you edit

– marvinIsSacul
Nov 19 '18 at 7:24





And I do agree with that. All I was suggesting was that you edit

– marvinIsSacul
Nov 19 '18 at 7:24




1




1





That would more or less be editing your answer to look like the OP's code. What would be the point of that? Before the OP has done some debugging, any answer would just be stabs in the dark. Let's wait until the OP has updated the question with all the relevant information.

– Magnus Eriksson
Nov 19 '18 at 9:37





That would more or less be editing your answer to look like the OP's code. What would be the point of that? Before the OP has done some debugging, any answer would just be stabs in the dark. Let's wait until the OP has updated the question with all the relevant information.

– Magnus Eriksson
Nov 19 '18 at 9:37


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53369556%2fhow-to-upload-image-in-the-storage-directory-in-php-it-gives-me-error-image-is%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?