沃梦达 / 编程问答 / php问题 / 正文

在 Laravel 4 中上传多个文件

Upload multiple files in Laravel 4(在 Laravel 4 中上传多个文件)

本文介绍了在 Laravel 4 中上传多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用于上传多个文件的控制器代码,我正在从 Google Chrome 上的邮递员"rest API 客户端传递密钥和值.我正在从邮递员添加多个文件,但只有一个文件正在上传.

Here is my controller code for uploading multiple files and I am passing key and value from 'postman' rest API client on Google Chrome. I am adding multiple files from postman but only 1 file is getting upload.

public function post_files() {
    $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 = 'upload/'.$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 ) {
                    $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
                // return Redirect::to('authors')
                } else {
                    return Response::json('error', 400);
                }
            }
        }
    }  
}

我也尝试过这段代码,但它返回了临时文件夹中文件的位置

I have tried this code also but it returns me location of a file in temporary folder

$file = Input::file('file');
            echo count($file);

回声计数($_FILES['file']);总是返回我 5.谁能告诉我为什么?

and echo count($_FILES['file']); returns me always 5.Can anyone tell me why?

以及为什么 foreach(Input::file('file') as $key => $abc) 给出错误无效参数

and why foreach(Input::file('file') as $key => $abc) gives the error invalid arguments

推荐答案

解决方案:

您只需执行以下操作即可获取所有文件:

Solution:

You can get all files by simply doing:

$allFiles = Input::file();

解释:

Input 类实际上是 IlluminateHttpRequest 类的外观(是的,就像 Request 外观一样 - 它们都是充当同一个班级的面孔"!**).

Explanation:

The class Input is actually a Facade for the IlluminateHttpRequest class (Yes, just like the Request facade - they both serve as the "Face" for the same class!**).

这意味着您可以使用 Request 中可用的任何方法.

That means you can use any methods available in Request .

如果我们搜索函数 file(),我们看到它是这样工作的:

If we search for the function file(), we see it works like this:

public function file($key = null, $default = null)
{
    return $this->retrieveItem('files', $key, $default);
}

现在,retrieveItem() 是一个受保护的方法,所以我们不能直接从控制器调用它.然而,深入研究,我们看到 我们可以传递 文件() 方法为null" 键.如果我们这样做,那么我们将获得所有物品!

Now, retrieveItem() is a protected method, so we can't just call it from our Controller directly. Looking deeper, however, we see that we can pass the file() method "null" for the key. If we do so, then we'll get all the items!

protected function retrieveItem($source, $key, $default)
{
    if (is_null($key))
    {
        return $this->$source->all();
    }
    else
    {
        return $this->$source->get($key, $default, true);
    }
}

所以,如果我们调用 Input::file(),Request 类将在内部运行 $this->retrieveItem('files', null, null)这将依次运行 return $this->files->all(); 我们将上传所有文件.

So, if we call Input::file(), the Request class will internally run $this->retrieveItem('files', null, null) which will in turn run return $this->files->all(); and we will get all the files uploaded.

** 注意 Input Facade 有额外的方法 get() 在里面可用.

** Note that Input Facade has the extra method get() available in it.

这篇关于在 Laravel 4 中上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 Laravel 4 中上传多个文件

基础教程推荐