用PHP上传文件不起作用

时间:2017-02-04 13:10:01

标签: php file upload

所以我遇到了将文件上传到我想要的地方的问题 它。它执行一切,但它不上传文件。它只是 说:

  

您的档案出了问题。

这是我的代码: 谢谢你的帮助!

<p>The file must be an image and less than 2 MB.</p>
<form action="filerupp.php" method="post" enctype="multipart/form-data">
    <p>Choose file:<br/>
    <input type="file" name="fileToUpload" id="fileToUpload"><br/>
    <input type="submit" value="Upload image" name="submit"></p>
</form>
<p>Files are <a href='pictures'>here</a>.</p>

这是我的PHP代码:

$target_dir = "pictures/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "<p>The File is an image" . $check["mime"] . ".<br/>";
        $uploadOk = 1;
    } else {
        echo "<p>The File is not an image.<br/>";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "<p>Sorry, the file already exists.<br/>";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, the file är för stor.<br/>";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF.<br/>";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file cannot be uploaded.</p>";

// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "<p>The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded and is <a href='pictures'>here</a>.</p>";
    } else {
        echo "<p>Sorry, there was something wrong with your file.</p>";
    }
}

2 个答案:

答案 0 :(得分:0)

此脚本的文件大小上传限制只有大约半个磁盘,因此您的图像大于此值。即使是2兆字节也是一个非常小的限制,可能最好使它达到10兆字节。

答案 1 :(得分:0)

所以我终于通过我看到的评论找到了问题。感谢所有试图提供帮助的人。我的代码没有错,这是我自己的freakin文件夹的权限。我检查了服务器权限,他们没事,但我没有检查我自己的文件夹-_-谢谢你,问题解决了!