简体   繁体   English

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

[英]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警告:move_uploaded_file(/uploads53e866b24977d1.48375376.pdf): 无法打开 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():无法将“C:\xampp\tmp\php7D69.tmp”移动到 C 中的“/uploads53e866b24977d1.48375376.pdf”:\xampp\htdocs\file_upload\upload.php 第 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";
                        }
                }
            }
        }
}

Probably SELinux did block the file from be written. SELinux可能阻止了文件的写入。 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. 如果是这种情况,则应将文件的上下文更改为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

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

$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) 我遇到了同样的问题,并且找到了解决此错误的方法,它位于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. 事实证明,您需要使用一些设置的完整路径,我使用XAMPP并存在相同的问题。 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> 相反,您可以在</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";
        }
}
}

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 filename 添加权限。 chmod -rwx directoryname to remove permissions. chmod -rwx 目录名删除权限。 chmod +x filename to allow executable permissions. chmod +x filename 允许可执行权限。 ... 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.

暂无
暂无

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

相关问题 上传文件警告:move_uploaded_file(../ view / pictureswhy.PNG):无法打开流:权限被拒绝 - Upload file Warning: move_uploaded_file(../view/pictureswhy.PNG): failed to open stream: Permission denied PHP警告:move_uploaded_file():无法移动/无法打开流:权限被拒绝 - PHP Warning: move_uploaded_file(): Unable to move/failed to open stream: Permission denied move_uploaded_file无法打开流和权限被拒绝错误 - move_uploaded_file failed to open stream and Permission denied error 警告:move_uploaded_file无法打开流:权限被拒绝的PHP文件 - Warning: move_uploaded_file failed to open stream: Permission denied PHP file PHP / IIS copy()/ move_uploaded_file()无法打开流:权限被拒绝警告 - PHP/IIS copy()/move_uploaded_file() failed to open stream: Permission denied Warning php 8.1 Mac 11.6.6 move_uploaded_file“无法打开 stream:权限被拒绝”错误 - php 8.1 Mac 11.6.6 move_uploaded_file "failed to open stream: Permission denied" error move_uploaded_file 无法打开 stream:权限被拒绝 - Mac - move_uploaded_file failed to open stream: Permission denied - Mac move_uploaded_file 无法打开流:权限被拒绝 Mac - move_uploaded_file failed to open stream: Permission denied Mac move_uploaded_file给出:无法打开流:权限被拒绝 - move_uploaded_file gives: failed to open stream: Permission denied 警告:move_uploaded_file(../ project / discussdesk.xlsx):无法打开流:权限被拒绝 - Warning: move_uploaded_file(../project/discussdesk.xlsx): failed to open stream: Permission denied
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM