php php文件上传错误警告move_uploaded_file无法打开流权限被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25237387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:44:36  来源:igfitidea点击:

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

phpfile-uploadfile-ioxampp

提问by usama

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

警告:move_uploaded_file(/uploads53e866b24977d1.48375376.pdf):无法打开流:C:\xampp\htdocs\file_upload\upload.php 中第 28 行的权限被拒绝

警告: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

这是我的 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

这是我的 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";
                        }
                }
            }
        }
}

采纳答案by Prateik Darji

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>

之后你不需要重定向页面;相反,您可以使用,在</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.

请注意,上传文件夹必须与您存储文件的目录位于同一目录中。

回答by Tikus.L4ju

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.

可能 SELinux 确实阻止了文件的写入。如果是这种情况,那么您应该将文件的上下文更改为 httpd_sys_rw_content_t,这意味着它将被转换为 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.

将此分配给您的应用程序可以在其中创建或修改文件的目录,或将其分配给 files 目录以允许您的应用程序修改它们。

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

回答by TBI

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

目录路径应该是 -uploads/如果它存在于您的 php 文件所在的同一目录中,并确保该目录具有777权限

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

回答by I am Cavic

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

我遇到了同样的问题,并找到了解决此错误的方法,它在 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.

事实证明,您需要一些设置的完整路径,我使用 XAMPP 并且遇到了同样的问题。玩了一段时间后,我决定将根连接到工作目录,结果很好。