2

What's wrong with this?

<form method="post" enctype="multipart/form-data" action="php/api/member_settings_profile_avatar.php">
    <input type="file" name="settings_choose_avatar" id="settings_choose_avatar">
    <input type="submit" value="Upload" />
</form>

php/api/member_settings_profile_avatar.php, line 2:

move_uploaded_file($_FILES["settings_choose_avatar"]["tmp_name"], "img/test.png");

I get the following error upon submitting a valid .png file:

 Warning: move_uploaded_file(img/test.png): failed to open stream: No such 
file or directory in /customers/4/1/a/mysitenamehere.com/httpd.www/php/api
/member_settings_profile_avatar.php on line 2 Warning: move_uploaded_file():
 Unable to move '/customers/4/1/a/mysitenamehere.com/tmp/phpeN9wyk' to 
'img/test.png' in /customers/4/1/a/mysitenamehere.com/httpd.www/php/api
/member_settings_profile_avatar.php on line 2

Is it an issue with my host or what?

7
  • 3
    Does the path exist? Have you created the path before trying to upload (mkdir)? Is the path right? Commented Jan 16, 2015 at 13:33
  • 1
    If both answers to @TopQuestions questions are yes, have you check is_writable on the folder? Commented Jan 16, 2015 at 13:35
  • 1
    Are you sure your path is correct, do you want to store the image in the /php/api/img folder or did you perhaps mean /img (all relative to the web-root)? Commented Jan 16, 2015 at 13:37
  • I changed it to /img, same result. The img folder exists in the root. Commented Jan 16, 2015 at 13:44
  • 1
    The root of the web-server is not the same as the root of the file system. Where do you want to store the image? Commented Jan 16, 2015 at 13:45

1 Answer 1

1

The problem is the path of the destination should be relative to the root of the website. You can do it with $_SERVER['DOCUMENT_ROOT'] and adding the path to the folder where you want to upload the file.

move_uploaded_file($_FILES["settings_choose_avatar"]["tmp_name"], $_SERVER['DOCUMENT_ROOT'] . "/img/test.png");

It will work if your current php file is at the website's root folder.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.