当上传超过300K的照片然后失败时,xmlHttpRequest级别2重启多次

时间:2013-10-29 11:22:02

标签: php image-uploading xmlhttprequest-level2

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

<!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 K.B的照片,进度条达到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

但没有改变。

1 个答案:

答案 0 :(得分:0)

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

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

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