0

我不想做任何不寻常的事情,我只需要一个简单的带有 HTML5 进度条的 ajax 照片上传,所以我的代码很普通:

<!DOCTYPE html>
<html>
<head>
    <script>
        function _(el){
            return document.getElementById(el);
        }
        function uploadFile(){
            var file = _("file1").files[0];

            var formdata = new FormData();
            formdata.append("file1",file);
            var ajax = new XMLHttpRequest();
            ajax.upload.addEventListener("progress", progressHandler, false);
            ajax.addEventListener("load", complateHandler, false);
            ajax.addEventListener("error", errorHandler, false);
            ajax.addEventListener("abort", abortHandler, false);
            ajax.send(formdata);
        }
        function progressHandler(event){
            _("loaded_n_total").innerHTML = "Uploaded" +event.loaded+" bytes of "+event.total;
            var percent = (event.loaded / event.total) * 100;
            _("progressBar").value = Math.round(percent);
            _("status").innerHTML = Math.round(percent)+"% uploaded... please wait";
        }
        function complateHandler(event){
            _("status").innerHTML = event.target.responseText;  
            _("progressBar").value = 0;
        }
        function errorHandler(event){
            _("status").innerHTML = "Upload Failed";    
        }
        function abortHandler(event){
            _("status").innerHTML = "Upload Aborted";
        }
    </script>
</head>
<body>

    <h2>Upload Photos</h2>

    <form id="upload_form" enctype="multipart/form-data" method="POST">
        <input type="file" id="file1" multiple /><br/>
        <input type="button" value="Upload File" onclick="uploadFile()" />
        <progress id="progressBar" value="0" max="100" style="width:300px"></progress>
        <h3 id="status"></h3>
        <p id="loaded_n_total"></p>
    </form>


</body>
</html>

up.php:

<?php
set_time_limit(100000);
$fileName = $_FILES["file1"]["name"];
$fileTmpLoc = $_FILES["file1"]["tmp_name"];
$fileType = $_FILES["file1"]["type"];
$fileSize = $_FILES["file1"]["size"];
$fileErrorMsg = $_FILES["file1"]["error"];
if(!$fileTmpLoc){
echo "ERROR: Please browse for a file before clicking the upload button.";
exit();
}
if(move_uploaded_file($fileTmpLoc, "uploads/$fileName")){
echo "$fileName upload is complate";    
}else{
echo "move_uploaded_file function failed";
}
?>

一切都很顺利,除了尝试上传超过 300 KB 的照片时,进度条达到 70% 然后重新开始达到 65% 或者重新开始,最后失败了!!!

我尝试将以下内容添加到 .htaccess 文件中: php_value upload_max_filesize 20M php_value post_max_size 20M php_value max_execution_time 200 php_value max_input_time 200

但没有任何改变。

4

1 回答 1

0

您应该使用@getimagesize()函数来验证图像。如果它不是图像(不仅仅是扩展名),它将返回 false。

if(list($width, $height, $type, $attributes) = @getimagesize($_FILES['userfile']['tmp_name'])) 

也许您的错误来自内存限制值。它曾经是128M。尝试更改为 256M 以确保它不是问题。无论如何,如果是这样,您应该更改代码以使其更轻。

于 2013-10-29T11:37:38.810 回答