PHP上传move_uploaded_file不起作用

时间:2013-09-09 20:24:36

标签: php

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

1 个答案:

答案 0 :(得分:0)

Seconde 回答:

根据您的评论,我建议您在移动文件之前使用 file_exists()

检查上传文件夹中是否存在此文件

第一个回答:

正如我所看到的,您上传文件夹的路径存在问题:

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

如果没有检查权限。