2014-10-10 53 views
0

此腳本適用於IE,但不適用於Chrome。 (我削減了腳本以方便閱讀。)Chrome中的php圖像上傳錯誤

事實證明,當使用Chrome時,isset($_FILES['upfile']返回false,所以我得到一條錯誤消息,而使用IE它返回true。爲什麼?

HTML

<input type="file" name="upfile" onchange="SelectImage(this);"> 

的JavaScript

function SelectImage(obj) { 
    var req = new XMLHttpRequest(); 
    var data = new FormData(); 
    req.open('POST', 'upload_image.php', true); 
    req.setRequestHeader('Content-type', 'multipart/form-data'); 
    data.append('upfile', obj.files[0]); 
    req.send(data); 
    req.onreadystatechange = function() 
    { 
     if (req.readyState == 4 && req.status == 200) 
      console.log(req.responseText); 
    }; 
} 

PHP

<?php 
try { 
    if (!isset($_FILES['upfile']['error']) || is_array($_FILES['upfile']['error'])) 
     throw new RuntimeException('Invalid parameters.'); 
... much more (security) code ... 
    echo 'something'; 
} 
catch (RuntimeException $e) { 
    echo $e->getMessage(); 
} 
?> 

回答

0

您的條件的錯誤。 您的代碼:

if (!isset($_FILES['upfile']['error']) || is_array($_FILES['upfile']['error'])) 
    throw new RuntimeException('Invalid parameters.'); 

檢查它有沒有任何錯誤或錯誤是一個數組,然後拋出異常。 更改爲

if (isset($_FILES['upfile']['error']) || is_array($_FILES['upfile']['error'])) 
    throw new RuntimeException('Invalid parameters.'); 
+0

這不是原因。 – Dick 2014-10-13 08:36:59