使用php上传文件的错误

时间:2014-06-01 14:22:15

标签: php file upload

我最近尝试使用PHP脚本上传文件,但我无法找到它为什么没有发生。我对此并不了解。我修改了W3schools的代码。

 function uploadit($id)
       {
            $allowedExts = array("gif", "jpeg", "jpg", "png");
            $temp = explode(".", $_FILES[$id]["name"]);
            $extension = end($temp);
            if ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/jpg")|| ($_FILES["file"]["type"] == "image/pjpeg")|| ($_FILES["file"]["type"] == "image/x-png")|| ($_FILES["file"]["type"] == "image/png"))&& (in_array($extension, $allowedExts))
            {
                if ($_FILES[$id]["error"] > 0) 
                {
                    echo "Return Code: " . $_FILES[$id]["error"] . "<br>";
                    $GLOBALS['fail']=true;
                }
                else 
                {
                    echo "Upload: " . $_FILES[$id]["name"] . "<br>";
                    echo "Type: " . $_FILES[$id]["type"] . "<br>";
                    echo "Size: " . ($_FILES[$id]["size"] / 1024) . " kB<br>";
                    echo "Temp file: " . $_FILES[$id]["tmp_name"] . "<br>";
                    if (file_exists("upload/" . $_FILES[$id]["name"])) 
                    {
                        echo $_FILES[$id]["name"] . " already exists. ";
                    }
                    else 
                    {
                        copy($_FILES[$id]["tmp_name"], "upload/".$_FILES[$id]["name"]) or die("Could not copy $extension");
                                /*move_uploaded_file($_FILES[$id]["tmp_name"],"/upload/" . $_FILES[$id]["name"]); (**both doesnt work for me)***/
                        echo "Stored in: " . "upload/" . $_FILES[$id]["name"];
                        $folder= "upload/" . $_FILES[$id]["name"];
                        print_r($_FILES);
                        return $folder;
                    }
                }
            }
            else
            {
                echo "Invalid file";
                $GLOBALS['fail']=true;
            }
        }

该文件通过表单提交。像这样:

<form action="uploadfile.php" method="post" enctype="multipart/form-data"><label> App Source:</label>
             <input type="file" name="src" id="src" /> <input type="submit" value="Upload" />
        </form>

0 个答案:

没有答案