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