2013-10-29 87 views
0

我不想做任何不尋常的東西,我需要的是一個簡單的Ajax照片上傳與HTML5的進度條,所以我的代碼是如此平凡:xmlHttpRequest 2級開始上傳照片超過300K,然後失敗

<!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

,但什麼都沒有改變。

回答

0

您應該使用@getimagesize()函數來驗證圖像。如果它不是圖像(不只是擴展名),它將返回false。

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

也許你的錯誤來自內存限制值。它使用128M。嘗試更改爲256M以確保它不是問題。無論如何,如果是這樣,你應該改變你的代碼,使其更輕。