繁体   English   中英

laravel 4中的多个文件上传

[英]Multiple file upload in laravel 4

我需要在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个文件已选中',输入类型为文件

将此部分调用为循环

   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_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++;
        }
    }

}

在View部分我写了name=image而不是image[]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM