0

我正在尝试上传一个文件,并拥有它,以便重复的文件名将附加 _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 . "'";
        }
4

1 回答 1

0

第二个答案:

根据您的评论,我建议您在移动文件之前使用file_exists()检查该文件是否存在于您的上传文件夹中

第一个答案:

如我所见,您的上传文件夹的路径有问题:

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

如果没有检查权限。

于 2013-09-09T20:26:16.190 回答