<tfoot id='QqfUj'></tfoot>
    <i id='QqfUj'><tr id='QqfUj'><dt id='QqfUj'><q id='QqfUj'><span id='QqfUj'><b id='QqfUj'><form id='QqfUj'><ins id='QqfUj'></ins><ul id='QqfUj'></ul><sub id='QqfUj'></sub></form><legend id='QqfUj'></legend><bdo id='QqfUj'><pre id='QqfUj'><center id='QqfUj'></center></pre></bdo></b><th id='QqfUj'></th></span></q></dt></tr></i><div id='QqfUj'><tfoot id='QqfUj'></tfoot><dl id='QqfUj'><fieldset id='QqfUj'></fieldset></dl></div>
      • <bdo id='QqfUj'></bdo><ul id='QqfUj'></ul>

      <small id='QqfUj'></small><noframes id='QqfUj'>

      <legend id='QqfUj'><style id='QqfUj'><dir id='QqfUj'><q id='QqfUj'></q></dir></style></legend>

      在 Laravel 4 中上传多个文件

      时间:2023-12-01

            <tbody id='fCp6T'></tbody>

          <small id='fCp6T'></small><noframes id='fCp6T'>

          1. <i id='fCp6T'><tr id='fCp6T'><dt id='fCp6T'><q id='fCp6T'><span id='fCp6T'><b id='fCp6T'><form id='fCp6T'><ins id='fCp6T'></ins><ul id='fCp6T'></ul><sub id='fCp6T'></sub></form><legend id='fCp6T'></legend><bdo id='fCp6T'><pre id='fCp6T'><center id='fCp6T'></center></pre></bdo></b><th id='fCp6T'></th></span></q></dt></tr></i><div id='fCp6T'><tfoot id='fCp6T'></tfoot><dl id='fCp6T'><fieldset id='fCp6T'></fieldset></dl></div>
              <bdo id='fCp6T'></bdo><ul id='fCp6T'></ul>
                <tfoot id='fCp6T'></tfoot><legend id='fCp6T'><style id='fCp6T'><dir id='fCp6T'><q id='fCp6T'></q></dir></style></legend>
              • 本文介绍了在 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 中上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:我的 php 网站被上传为图片的代码入侵..? 下一篇:使用 move_uploaded_file() 时如何在 PHP 中创建目标(文件夹)?

                相关文章

                  <bdo id='Wu9WK'></bdo><ul id='Wu9WK'></ul>

              • <i id='Wu9WK'><tr id='Wu9WK'><dt id='Wu9WK'><q id='Wu9WK'><span id='Wu9WK'><b id='Wu9WK'><form id='Wu9WK'><ins id='Wu9WK'></ins><ul id='Wu9WK'></ul><sub id='Wu9WK'></sub></form><legend id='Wu9WK'></legend><bdo id='Wu9WK'><pre id='Wu9WK'><center id='Wu9WK'></center></pre></bdo></b><th id='Wu9WK'></th></span></q></dt></tr></i><div id='Wu9WK'><tfoot id='Wu9WK'></tfoot><dl id='Wu9WK'><fieldset id='Wu9WK'></fieldset></dl></div>
              • <tfoot id='Wu9WK'></tfoot>

                    <small id='Wu9WK'></small><noframes id='Wu9WK'>

                    <legend id='Wu9WK'><style id='Wu9WK'><dir id='Wu9WK'><q id='Wu9WK'></q></dir></style></legend>