Since recently I am working with PHP and I came across a problem with uploading an image.
My code works well on my local machine; however, when uploaded on my VM the file is not getting uploaded to the tmp folder.
I have tried to see the array that is being filled from the $_FILES with “print_r($_FILES);” and this is what I get in the array:
array ( [image] => array ( [name] => avatar-1.jpg [type] => image/jpeg [tmp_name] => /tmp/phpfkhvrw [error] => 0 [size] => 1029 ) )
I have reviewed the php.ini file and everything seems to be in order. I have also checked a few other articles whit similar issues, but non of the suggestions worked.
I think that it is something to do with the permission as in the envvars file the configuration is the following:
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data
However, all of the files on the server are with root as user and group.
I really hope that someone can share some knowledge on this as I really don’t know what to do next.
3
Answers
It turns out that the file was being uploaded in the tmp dir but not being moved as the owner and group of the html and tmp folder was root.
Changed the owner and group of both of the folder and now everything works.
sudo chown www-data:www-data FolderName -R
Alternatively, permissions 777 can be used on all folders, but it is not recommended.
What you have shown us here proves that the file upload was successful. You need to be a lot more specific as to what you think has failed.
As Zeusarm hints at, the file which has been created (/tmp/phpfkhvrw) does not persist after the HTTP request finishes – it is removed by PHP. If you want to retain this faile after the request then you need to call move_uploaded_file()
You might want to work on your permissions model.
Maybe add chmod for your function. Example.