2015-11-02 52 views
-2
form action="upload" enctype="multipart/form-data" method="post"> 
<input id="file" name="file" type="file" /> 
<input id="Submit" name="submit" type="submit" value="Submit" /> 
</form> 

在服务器侧获取错误而上传()

include 'reqFunctions.php'; 
$uploaddir = '/path/'; 
$idx = "file"; 
$res = array("success" => true, "status" =>array()); 
if (isset($_FILES[$idx]) && is_array($_FILES[$idx])) { 
    foreach ($_FILES[$idx]["error"] as $key => $error) { 
      $status = array("success" => true); 
     if ($error == UPLOAD_ERR_OK) { 
      $tmp_name = $_FILES[$idx]["tmp_name"][$key]; 
      $name = $_FILES[$idx]["name"][$key]; 
      $name = $_FILES[$idx]["name"][$key]; 
      $extension=end(explode(".", $name)); 
      $newfilename=generateRandomString(10)."_".$timestamp1."_".generateRandomString(10).$extension; 
      if (move_uploaded_file($tmp_name, $uploaddir.$newfilename)) { 
       $status["message"] = "ok"; 
       $status["path"]="images.smsiland.com/post/large/".$newfilename; 
      } else { 
       $res["success"] = false; 
       $status["success"] = false; 
       $status["error"] = error_get_last(); 
       $status["message"] = "internal server error"; 
      } 
     } else { 
      $res["success"] = false; 
      $status["success"] = false; 
      $status["error"] = $error; 
      $status["message"] = "upload error"; 
     } 
     $res["status"][] = $status; 
    } 
} 

echo(json_encode($res)); 


警告提供的foreach文件无效的参数:对的foreach()无效的论点提供在/upload.php上线
{“success”:true,“status”:[]}

+3

是'$ _FILES [$ IDX] [ “错误”]'数组?我不这么认为。 – Daan

+0

请SSEE全码http://pastebin.com/zKjgZbP8 – xrcwrn

+1

'$ _FILES [$ IDX] [ “错误”]'是一个数字,而不是阵列。阅读[文档](http://php.net/manual/en/features.file-upload.errors.php)。 – axiac

回答

1

before before ACH线路码 因为没有阵列中$ _FILES存在[$ IDX];它返回0

<?php 
$uploaddir = '/upload/'; 
    $idx = "file"; 
    //echo "<pre>";print_r($_FILES);exit; 
    $res = array("success" => true, "status" =>array()); 
    if (isset($_FILES[$idx]['name']) && is_array($_FILES[$idx])) { 
     if(is_array($_FILES[$idx]["error"])){ 
      foreach ($_FILES[$idx]["error"] as $key => $error) { 
       $status = array("success" => true); 
      if ($error == UPLOAD_ERR_OK) { 
       //move upload codde goes here 
       } 
      } 
     }else{ 
      //echo "your code "; 
     } 

      } 

      ?> 
1

一个foreach声明仅适用于数组,你的代码并没有引起$FILES数组包含数组,因为你还没有告诉你希望它是一个数组的HTML。

浏览器正在返回$FILES阵列像这样:

Array 
(
    [file] => Array 
     (
      [name] => xxx.bmp 
      [type] => image/bmp 
      [tmp_name] => \tmp\php73AB.tmp 
      [error] => 0 
      [size] => 78918 
     ) 

) 

因为你有文件阵列中使用这个HTML语句

<input id="file" name="file" type="file" /> 

注意所有的字段是标量场,这是造成在foreach ($_FILES[$idx]["error"] as $key => $error) {生成一个警告,然后不能执行foreach语句里面什么。

foreach ($_FILES[$idx]["error"] as $key => $error) { 
    $status = array("success" => true); 
    if ($error == UPLOAD_ERR_OK) { 
      . . 
    } 
} 

我asssuming这个代码只是第1步,最终要允许脚本上传多个文件。如果是这样,那么简单的解决办法是改变HTML的<input type="file"...标签这样

<input id="file" name="file[]" type="file" /> 

那么浏览器就会产生$_FILES阵列这样

Array 
(
    [file] => Array 
     (
      [name] => Array 
       (
        [0] => xxx.bmp 
       ) 
      [type] => Array 
       (
        [0] => image/bmp 
       ) 
      [tmp_name] => Array 
       (
        [0] => \tmp\phpCABF.tmp 
       ) 
      [error] => Array 
       (
        [0] => 0 
       ) 
      [size] => Array 
       (
        [0] => 78918 
       ) 
     ) 
) 

您的代码可以编译为$FILES数组中的每个字段本身也不是数组。

另外,您可以添加另一

<input id="file1" name="file[]" type="file" /> 
<input id="file2" name="file[]" type="file" /> 

这将有同样的效果。

+0

但为什么它正如我在回答的开头说的是给错误 – xrcwrn

+0

。因为使用了'<输入的ID =“文件”名称=“文件”类型=“文件” />'当浏览器建立将填充'$ _FILES'数组中的数据数组内的数据被创建为标量变量而不是一组子阵列。因此'foreach($ _FILES [$ idx] [“error”] ....'不是数组,因此PHP错误是因为'foreach'只能用在数组上 – RiggsFolly

+0

php代码没有任何问题只需更新输入 – xrcwrn