簡體   English   中英

PHP上傳move_uploaded_file不起作用

[英]PHP Upload move_uploaded_file not working

我正在嘗試上傳一個文件,並具有一個文件名,以便重復的文件名將附加_0,_1,_2等。輸入重復的文件時,出現此錯誤:

Warning: move_uploaded_file(../data/uploads/../data/uploads/Penguins_0.jpg): failed to open stream: No such file or directory in D:\xampp\htdocs\staff\upload_form.php on line 29

Warning: move_uploaded_file(): Unable to move 'D:\xampp\tmp\php7150.tmp' to '../data/uploads/../data/uploads/Penguins_0.jpg' in D:\xampp\htdocs\staff\upload_form.php on line 29
Upload failed: Could not move uploaded file. [../data/uploads/Penguins_0.jpg]

有人可以幫忙嗎?

$id = "img_" . $_POST['eqId'] . "_" . $_POST['imgId'];
        $file = $_FILES[$id];
        $allowedExts = array("gif", "jpeg", "jpg", "png");
        $temp = explode(".", $file["name"]);
        $extension = strtolower(end($temp));
        $dir = "../data/uploads/";
        if(($file["type"] == "image/gif")
        || ($file["type"] == "image/jpeg")
        || ($file["type"] == "image/jpg")
        || ($file["type"] == "image/pjpeg")
        || ($file["type"] == "image/x-png")
        || ($file["type"] == "image/png")
        && in_array($extension, $allowedExts)){
            if ($file["error"] > 0){
                echo "There was an error uploading the file. [Code: " . $file["error"] . "]";
            }else{
                $fileName = $file['name'];
                if (file_exists($dir . $file["name"])){
                    $appendCount = 0;
                    while(file_exists($dir . $temp[0] . "_" . $appendCount . "." . $extension)){
                        $appendCount++;
                    }
                    $fileName = ($dir . $temp[0] . "_" . $appendCount . "." . $extension);
                }

                if(move_uploaded_file($file["tmp_name"], $dir . $fileName )){
                    echo "success:" . $file["tmp_name"];
                }else{
                    echo "Upload failed: Could not move uploaded file. [" . $fileName . "]";
                }

            }
        }else{
            echo "Invalid file type '" . $extension . "'";
        }

Seconde 答案:

根據您的評論,我建議您在移動文件之前使用file_exists()檢查此文件拳頭是否存在於您的上傳文件夾中

第一個答案:

如我所見,您的上傳文件夾的路徑有問題:

../data/uploads/../data/uploads/

如果沒有,請檢查權限。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM