skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.


  2. 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()

    all of the files on the server are with root as user and group

    You might want to work on your permissions model.

    Login or Signup to reply.
  3. Maybe add chmod for your function. Example.

       // move_uploaded_file has is_uploaded_file() built-in
                    if(move_uploaded_file($tmp_file, $file_path)) {
                    echo "File moved to: {$file_path}<br />";
    
                    // remove execute file permissions from the file
                        if(chmod($file_path, 0644)) {
                            echo "Execute permissions removed from file.<br />";
                            $file_permissions = file_permissions($file_path);
                            echo "File permissions are now '{$file_permissions}'.<br />";
                        } else {
                            echo "Error: Execute permissions could not be removed.<br />";
                        }
                }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search