david
david

Reputation: 292

PHP FileUpload error

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?

Upvotes: 2

Views: 85

Answers (1)

Skatox
Skatox

Reputation: 4284

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.

Upvotes: 1

Related Questions