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

Deadly 提交于 2020-01-15 11:35:12

问题


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";
                        }
                }
            }
        }
}

回答1:


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.




回答2:


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



回答3:


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";
}



回答4:


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.



来源:https://stackoverflow.com/questions/25237387/php-file-upload-error-warning-move-uploaded-file-failed-to-open-stream-permissio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!