繁体   English   中英

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

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

这是两个错误

警告:move_uploaded_file(/uploads53e866b24977d1.48375376.pdf): 无法打开 stream: Permission denied in C:\xampp\htdocs\file_upload\upload.php on line 28

警告:move_uploaded_file():无法将“C:\xampp\tmp\php7D69.tmp”移动到 C 中的“/uploads53e866b24977d1.48375376.pdf”:\xampp\htdocs\file_upload\upload.php 第 28 行

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

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

SELinux可能阻止了文件的写入。 如果是这种情况,则应将文件的上下文更改为httpd_sys_rw_content_t,这意味着它将被转到Apache使用的可读可写目录和文件。

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

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

目录路径应为uploads/如果它位于您的php文件所在的目录中),并确保此目录具有777权限

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

我遇到了同样的问题,并且找到了解决此错误的方法,它位于move_uploaded_file($file_tmp, $file_destination)

添加以下内容

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

事实证明,您需要使用一些设置的完整路径,我使用XAMPP并存在相同的问题。 玩了一会儿之后,我决定将根连接到工作目录,结果很好。

我尝试了相同的代码,它对我有用。 我为您做了一些更改。

<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>

之后,您无需重定向页面; 相反,您可以在</form>下使用this

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

请注意,上载文件夹必须与文件存储在同一目录中。

chmod +rwx filename 添加权限。 chmod -rwx 目录名删除权限。 chmod +x filename 允许可执行权限。 ... 例如:

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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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