在 Laravel 4 中上传多个文件

2023-12-01php开发问题
1

本文介绍了在 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 中上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17