简体   繁体   中英

php file upload error warning move_uploaded_file failed to open stream permission denied in

Here are the two errors

Warning: move_uploaded_file(/uploads53e866b24977d1.48375376.pdf): failed to open stream: Permission denied in C:\xampp\htdocs\file_upload\upload.php on line 28

Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\php7D69.tmp' to '/uploads53e866b24977d1.48375376.pdf' in C:\xampp\htdocs\file_upload\upload.php on line 28

Here is my HTML

<form method="POST" action="upload.php" enctype="multipart/form-data">
        <label for="">Upload Your Cv</label><input type="file" name="file">
        <input type="submit" value="upload">
</form>

Here is my PHP

if (isset($_FILES['file'])) {
        $file   = $_FILES['file'];
        // print_r($file);  just checking File properties

        // File Properties
        $file_name  = $file['name'];
        $file_tmp   = $file['tmp_name'];
        $fiel_size  = $file['size'];
        $file_error = $file['error'];

        // Working With File Extension
        $file_ext   = explode('.', $file_name);
        $file_fname = explode('.', $file_name);

        $file_fname = strtolower(current($file_fname));
        $file_ext   = strtolower(end($file_ext));
        $allowed    = array('txt','pdf','doc');

        if (in_array($file_ext,$allowed)) {
            if ($file_error === 0) {
                if ($fiel_size <= 5000000) {
                        // $file_name_new     =  $file_fname . uniqid('',true) . '.' . $file_ext;
                        $file_name_new    =  uniqid('',true) . '.' . $file_ext;
                        $file_destination =  '/uploads' . $file_name_new;
                        // echo $file_destination;
                        if (move_uploaded_file($file_tmp, $file_destination)) {
                                echo "Cv uploaded";
                        }
                }
            }
        }
}

Probably SELinux did block the file from be written. If this is the case then you should change the context of the file to httpd_sys_rw_content_t which means it will be turned to a readable and writable directories and files used by Apache.

Assign this to directories where files can be created or modified by your application, or assign it to files directory to allow your application to modify them.

sudo chcon -t httpd_sys_rw_content_t /var/www/html/path/to/writable/folder -R

Directory path should be - uploads/ if it is present in same directory where your php file is and make sure this directory has 777 permission

$file_destination =  'uploads/' . $file_name_new;
if (move_uploaded_file($file_tmp, $file_destination)) {
    echo "Cv uploaded";
}

I had the same problem and have found the solution to this error, it is inside the move_uploaded_file($file_tmp, $file_destination)

add the following

$root = getcwd();
move_uploaded_file($file_tmp, $root.$file_destination)

It turns out you need the full path with some setup, I use XAMPP and had same problem. After playing around for a while I decided to concatenate the root to working directory and it worked out fine.

I have tried the same code and it works for me. I have made some changes for you.

<form method="POST" enctype="multipart/form-data">
    <label for="">Upload Your Cv</label><input type="file" name="file">
    <input type="submit" name="upload" value="upload">
</form>

After that you don't need to redirect the page; instead you can use this , below the </form>

if(isset($_REQUEST["upload"]))
{
if (isset($_FILES['file'])) {
        $file   = $_FILES['file'];
        // print_r($file);  just checking File properties

        // File Properties
        $file_name  = $file['name'];
        $file_tmp   = $file['tmp_name'];
        $file_size  = $file['size'];
        $file_error = $file['error'];

        // Working With File Extension
        $file_ext   = explode('.', $file_name);
        $file_fname = explode('.', $file_name);

        $file_fname = strtolower(current($file_fname));
        $file_ext   = strtolower(end($file_ext));
        $allowed    = array('txt','pdf','doc','ods');


        if (in_array($file_ext,$allowed)) {
            //print_r($_FILES);


            if ($file_error === 0) {
                if ($file_size <= 5000000) {
                        $file_name_new     =  $file_fname . uniqid('',true) . '.' . $file_ext;
                        $file_name_new    =  uniqid('',true) . '.' . $file_ext;
                        $file_destination =  'upload/' . $file_name_new;
                        // echo $file_destination;
                        if (move_uploaded_file($file_tmp, $file_destination)) {
                                echo "Cv uploaded";
                        }
                        else
                        {
                            echo "some error in uploading file".mysql_error();
                        }
//                        
                }
                else
                {
                    echo "size must bne less then 5MB";
                }
            }

        }
        else
        {
            echo "invalid file";
        }
}
}

Note that the upload folder must be in the same directory as where you store the file.

chmod +rwx filename to add permissions. chmod -rwx directoryname to remove permissions. chmod +x filename to allow executable permissions. ... For example:

chmod 777 foldername will give read, write, and execute permissions for everyone.
chmod 700 foldername will give read, write, and execute permissions for the user only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM