0

我需要在 laravel 4 中上传多个文件 我在控制器中编写了以下代码

public function post_files()
        {
            $allowedExts = array("gif", "jpeg", "jpg", "png","txt","pdf","doc","rtf");
$temp = explode(".", $_FILES["file"]["name"]);
            $extension = end($temp);
            $filename= $temp[0];
            $destinationPath = 'upload/'.$filename.'.'.$extension;




            if(in_array($extension, $allowedExts)&&($_FILES["file"]["size"] < 20000000))
              {
                  if($_FILES["file"]["error"] > 0)
                {
                        echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
                }

                  else
                  {
                      if (file_exists("upload/" . $_FILES["file"]["name"]))
                     {
                        echo $_FILES["file"]["name"] . " already exists. ";
                     }
                    else
                    {
                        $uploadSuccess=move_uploaded_file($_FILES["file"]["tmp_name"],$destinationPath);
                        if( $uploadSuccess ) 
                        {
                           $document_details=Response::json(Author::insert_document_details_Call($filename,$destinationPath));
                           return $document_details; // or do a redirect with some message that file was uploaded
                        } 
                        else 
                        {
                           return Response::json('error', 400);
                        }
                    }

                }

            }
            else
            {
                return "Invalid file";
            }
        }

        }

我可以通过此代码上传单个文件,但无法上传多个文件。请在这个问题上帮助我..提前谢谢你..


public function post_abc()
        {
            // $file = Input::file('file'); // your file upload input field in the form should be named 'file'
            $allowedExts = array("gif", "jpeg", "jpg", "png","txt","pdf","doc","rtf","docx","xls","xlsx");


            foreach($_FILES['file'] as $key => $abc)
            { 
            $temp = explode(".", $_FILES["file"]["name"]);
            $extension = end($temp);
            $filename= $temp[0];
            $destinationPath = 'abc/'.$filename.'.'.$extension;

            if(in_array($extension, $allowedExts)&&($_FILES["file"]["size"] < 20000000))
              { 
                  if($_FILES["file"]["error"] > 0)
                  {
                     echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
                  }

                     if (file_exists($destinationPath))
                      {
                             echo $filename." already exists. ";
                      }
                      else
                      {
                        $uploadSuccess=move_uploaded_file($_FILES["file"]["tmp_name"],$destinationPath);

                        if( $uploadSuccess )
                        {
                           return "hello";
                        } 
                        else 
                        {
                             return Response::json('error', 400);
                        }
                      }
                }
            }

        }

在谷歌浏览器上使用邮递员休息客户端我已将密钥作为“文件”传递,值作为“3 个选定文件”传递,输入类型为文件

4

2 回答 2

1

将此部分调用为循环

   if(in_array($extension, $allowedExts)&&($_FILES["file"]["size"] < 20000000))
              {
                  if($_FILES["file"]["error"] > 0)
                {
                        echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
                }

                  else
                  {
                      if (file_exists("upload/" . $_FILES["file"]["name"]))
                     {
                        echo $_FILES["file"]["name"] . " already exists. ";
                     }
                    else
                    {
                        $uploadSuccess=move_uploaded_file($_FILES["file"]["tmp_name"],$destinationPath);
                        if( $uploadSuccess ) 
                        {
                           $document_details=Response::json(Author::insert_document_details_Call($filename,$destinationPath));
                           return $document_details; // or do a redirect with some message that file was uploaded
                        } 
                        else 
                        {
                           return Response::json('error', 400);
                        }
                    }

                }

            }
            else
            {
                return "Invalid file";
            }
于 2013-08-12T12:51:00.867 回答
1

问题解决了。我在控制器中更改了如下代码:

public function post_multiple()
{
        // $file = Input::file('file'); // your file upload input field in the form should be named 'file'
    $allowedExts = array("gif", "jpeg", "jpg", "png","txt","pdf","doc","rtf","docx","xls","xlsx");
    if(isset($_FILES['image']['tmp_name']))
    {
        $i=0;
        foreach(Input::file('image') as $key => $file)
        { 
            $temp = explode(".", $_FILES["image"]["name"][$i]);
            $extension = end($temp);
            $filename= $temp[0];
            $destinationPath = 'abc/'.$filename.'.'.$extension;
            if(in_array($extension, $allowedExts)&&($_FILES["image"]["size"][$i] < 20000000))
            { 
                if($_FILES["image"]["error"][$i] > 0)
                {
                    echo "Return Code: " . $_FILES["image"]["error"][$i] . "<br>";
                }

                if (file_exists($destinationPath))
                {
                    echo $filename." already exists. ";
                }
                else
                { 
                    $uploadSuccess=move_uploaded_file($_FILES["image"]["tmp_name"][$i],$destinationPath);

                    if( $uploadSuccess )
                    {
                        $name=$filename.".".$extension;
                        $document_details=Response::json(Author::insert_document_details_Call($name,$destinationPath));
                        echo "Update";
                    } 
                    else 
                    {
                        return Response::json('error', 400);
                    }
                }
            }

            $i++;
        }
    }

}

在查看部分我写name=image而不是image[]

于 2013-08-13T12:59:22.053 回答