在Laravel 5.1中处理多个文件(作为API) [英] Processing multiple files in Laravel 5.1 (as an API)

查看:40
本文介绍了在Laravel 5.1中处理多个文件(作为API)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这似乎是一件很基本的事情,但是我在网上找不到很多有关正在发生的事情的文档....

So this seems like a pretty basic thing but I can't find a lot of documentation online about what's going on....

我正在尝试使用Laravel 5.1浏览文件列表,我只能返回/处理/查看第一个文件.我正在使用Postman将请求发送到API(因此我知道在POST请求中启用了multiple),然后通过几种不同的方式进行迭代:

I'm trying to run through a list of files using Laravel 5.1 and I can only return/process/see the first file. I'm using Postman to send the request to the API (so I know multiple is enabled in the POST request) and then iterating through that a few different ways:

public function files(Request $request)
{
    foreach($request->files as $file)
    {
        var_dump($file);
    }
}

偶:

public function files()
{
    foreach($_FILES['files'] as $file)
    {
        var_dump($file);
    }
}

我总是返回(如果使用$request->files方法,则返回对象形式):

I'm always returning (or the object form if the $request->files method is used):

string 'Screen%20Shot%202015-10-23%20at%2010.07.23%20AM.png' (length=51)
string 'image/png' (length=9)
string '/tmp/phpZw1ALu' (length=14)
int 0
int 13687

为什么会这样?如何在Laravel 5.1的控制器中查看多个文件?

Why is this happening? What can I do to see multiple files in Laravel 5.1's controllers?

推荐答案

所以我用下面的代码files.php创建了一个崭新的页面,它现在返回所有三个文件(或者我上传的文件很多):

So I made a brand new page with the following code called files.php and it's now returning all three files (or however many I upload):

<!DOCTYPE>
<html>
<body>
 <form method="post" enctype="multipart/form-data" action="http://lucolo.dev/files">
   <input type="file" name="files[]" multiple>
   <input type="submit" value="Upload">
 </form>
</body>
</html>

错误是我在Postman中收到了POST请求,但只接受一个名为files而不是files[]的参数.一旦更改,我的Laravel控制器中的代码:

The error was that I was had the POST request in Postman only accepting a parameter named files instead of files[]. Once that was changed, the code in my Laravel controller:

public function files(Request $request)
{
    foreach($request->files as $file)
    {
        var_dump($file);
    }
}

现在返回:

array (size=3)
0 => 
object(Symfony\Component\HttpFoundation\File\UploadedFile)[84]
  private 'test' => boolean false
  private 'originalName' => string 'Screen Shot 2015-10-23 at 10.07.23 AM.png' (length=41)
  private 'mimeType' => string 'image/png' (length=9)
  private 'size' => int 270504
  private 'error' => int 0
  private 'pathName' (SplFileInfo) => string '/tmp/php1M5ZJl' (length=14)
  private 'fileName' (SplFileInfo) => string 'php1M5ZJl' (length=9)
1 => 
object(Symfony\Component\HttpFoundation\File\UploadedFile)[85]
  private 'test' => boolean false
  private 'originalName' => string 'Screen Shot 2015-10-26 at 7.28.59 PM.png' (length=40)
  private 'mimeType' => string 'image/png' (length=9)
  private 'size' => int 13687
  private 'error' => int 0
  private 'pathName' (SplFileInfo) => string '/tmp/phpE22ubf' (length=14)
  private 'fileName' (SplFileInfo) => string 'phpE22ubf' (length=9)
2 => 
object(Symfony\Component\HttpFoundation\File\UploadedFile)[86]
  private 'test' => boolean false
  private 'originalName' => string 'Screen Shot 2015-10-27 at 2.50.58 PM.png' (length=40)
  private 'mimeType' => string 'image/png' (length=9)
  private 'size' => int 786350
  private 'error' => int 0
  private 'pathName' (SplFileInfo) => string '/tmp/phph8v0C8' (length=14)
  private 'fileName' (SplFileInfo) => string 'phph8v0C8' (length=9)

希望对某人有帮助!

这篇关于在Laravel 5.1中处理多个文件(作为API)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆