PHP上传器的问题

时间:2015-11-15 04:01:27

标签: php forms

我有一个简单的表单,允许用户存储一些HTML代码,标题和图像。表单将数据发送到PHP,该PHP应该存储图像(和其他不相关的东西。)图像没有上传,但我没有收到任何错误。此工具仅用于为我提供添加内容的方法,而且其他任何人都无法访问,因此我还没有检查输入卫生设施。

HTML:

<form id="bpost_form" class="dropzone" method="post" action="writetofile.php" style="text-align:center" enctype="multipart/form-data">
            <input type="text" name="newpost_title"/>
            <br>
            <input type="file" name="img_upload" id="fileToUpload">
            <br>
            <textarea name="newpost_body" id="newpost" rows="10" cols="80">
                This is my textarea to be replaced with CKEditor.
            </textarea>
            <script>
                // Replace the <textarea id="editor1"> with a CKEditor
                // instance, using default configuration.
                CKEDITOR.replace('newpost');
            </script>
            <input type="submit"
                   value="Save"/>
        </form>

PHP:

#Image code
$target_dir = "../BlogPosts/post_images/";
$target_file = $target_dir . basename($_FILES["img_upload"]["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["img_upload"]["tmp_name"]);
    if ($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["img_upload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["img_upload"]["tmp_name"], $target_file)) {
        echo "The file " . basename($_FILES["img_upload"]["name"]) . " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

1 个答案:

答案 0 :(得分:0)

PHP可以在读取错误代码之前重定向。我使用的测试图像不正确,重定向阻止我看到正确的错误代码。通过删除

解决了这个问题
header('Location: /../index.html');

并使用不同的图像。