AJAX图像上传 - 无效的文件类型

时间:2016-11-03 21:21:15

标签: php jquery ajax

我一直在关注AJAX图像文件上传,我已经开始工作了,但是经过大量修改后才能使用我需要的东西。我上传了一个压缩的.jpg文件,但它直接进入" Invalid File"。

HTML

<label class="control-label">Thumbnail Name Image Name</label>
            <input id="filenamedb" name="filenamedb" type="text" class="form-control" value="" disabled>
            <form method="post" id="fileinfo" name="fileinfo" onsubmit="return submitForm();">
                <label>Select a File:</label><br>
                <input id="thefile" type="file" name="file" onchange="alertFilename()" required /><br/>
                <input type="submit" onclick="alertFilename()" value="Upload" />
            </form><br/>
            <progress></progress>

AJAX

<script type="text/javascript">
    function alertFilename()
    {
        var thefile2 = document.getElementById('thefile');
        document.getElementById('filenamedb').value = thefile2.files[0].name;
    }

    function submitForm()
    {
        console.log("submit event");
        var fd = new FormData(document.getElementById("thefile"));
        $.ajax({
        xhr: function() {
            var myXhr = $.ajaxSettings.xhr();
            if(myXhr.upload){
                myXhr.upload.addEventListener('progress',progressHandlingFunction, false);
            }
            return myXhr;
        },
        url: "/upload.php",
        type: "POST",
        data: fd,
        processData: false,
        contentType: false          
        }).done(function( data ) {
            console.log("PHP Output:");
            console.log( data );
        });
        return false;
    }

    function progressHandlingFunction(e){
        if(e.lengthComputable){
            $('progress').attr({value:e.loaded,max:e.total});
        }
    }
</script>

upload.php的

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["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"))
&& ($_FILES["file"]["size"] < 200000)
&& in_array($extension, $allowedExts)) {
    if ($_FILES["file"]["error"] > 0) {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    } else {
        $filename = $_FILES["file"]["name"];
        echo "Upload: " . $_FILES["file"]["name"] . "<br>";
        echo "Type: " . $_FILES["file"]["type"] . "<br>";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

        if (file_exists("/images/feed/" . $filename)) {
            echo $filename . " already exists. ";
        } else {
            move_uploaded_file($_FILES["file"]["tmp_name"],
            "/images/feed/" . $filename);
            echo "Stored in: " . "/images/feed/" . $filename;
        }
    }
} else {
    echo "Invalid file";
}
?>

0 个答案:

没有答案