上传文件时出错为foreach()提供的参数无效

时间:2015-11-02 10:16:40

标签: php

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));


 警告 /upload.php 12 上为foreach()提供的参数无效
   {&#34;成功&#34;:真,&#34;状态&#34;:[]}

2 个答案:

答案 0 :(得分:2)

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" />

注意files数组中的所有字段都是标量字段,导致foreach ($_FILES[$idx]["error"] as $key => $error) {生成警告,然后在foreach语句中生成 NOT EXECUTE

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

我觉得这段代码只是第1步,最后你想让脚本上传多个文件。如果是这种情况,则简单的解决方案是更改<input type="file"...标记的HTML,如此

<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" />

这会产生同样的效果。

答案 1 :(得分:1)

在foreach行代码之前

因为$ _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 ";
        }

          }

          ?>