在Laravel中的foreach循环中获取所有文件 [英] Get all files in a foreach loop in Laravel

查看:429
本文介绍了在Laravel中的foreach循环中获取所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些<input type="file">是由jQuery动态生成的. 这意味着每个inputname attribute也是动态生成的,是一个数字,并且每次在DOM中出现新的input时都会递增1.

I have some <input type="file"> that are generated dynamically by jQuery. That means that the name attribute of each input is also generated dynamically, is a number and increments by 1 every time a new input appears in the DOM.

我需要类似foreach loop的东西来获取我的php中的所有输入. 我尝试上传1个文件,但我留下了3个inputs为空,因为我的输入是连续的,我只想上传一个图像而不是4个.

I need something like a foreach loop to get all the inputs in my php. I have tried to upload 1 file and i have left 3 inputs empty because my inputs are in a row and i wanted to upload only one image and not 4. i looped through this $all = Input::file(); and the var_dump response is this

NULL object(Symfony\Component\HttpFoundation\File\UploadedFile)#9 (7) { ["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> bool(false) ["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(10) "index.jpeg" ["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(10) "image/jpeg" ["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(9408) ["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(0) ["pathName":"SplFileInfo":private]=> string(25) "/opt/lampp/temp/phpSVn3Tb" ["fileName":"SplFileInfo":private]=> string(9) "phpSVn3Tb" } NULL NUL L

我知道我的问题有点难以理解,但是我找不到更好的方法来解释问题.

I know that my question is a little bit difficult to understand, but i can not find a better way to explain the problem.

修改: 这些是我的默认输入:

These are my default inputs:

<div class="row">
<input type="file" name="1">
<input type="file" name="2">
<input type="file" name="3">
<input type="file" name="4">
</div

当用户按下按钮时,jQuery会添加

When the user presses a button, jQuery appends

<div class="row">
<input type="file" name="5">
<input type="file" name="6">
<input type="file" name="7">
<input type="file" name="8">
</div>

以及每按一次该按钮

<div class="row">
<input type="file" name="9">
<input type="file" name="10">
<input type="file" name="11">
<input type="file" name="12">
</div

然后在php中,我需要获取所有具有值的输入.

Then in php i need to get all the inputs that have a value.

我所做的是:

$all = Input::all()
foreach ($all as $one => $two) 
   var_dump($two);
}

推荐答案

您需要为多个文件上传创建一个表单:

You need to create a Form for multiple File uploads:

{!! Form::open(array('url'=>'url_to_controller','method'=>'POST', 'files'=>true)) !!}

您的所有文件都应使用特定的上传名称,例如:

And all your files should have a specfic name for the upload, like this:

{!! Form::file('files[]', array('multiple'=>true)) !!}

然后您可以使用控制器:

Then you can use your Controller:

class UploadController extends Controller {
   public function store() {

   $files = Input::file('files');

并遍历所有文件

 foreach($files as $file) {
   ....

这篇关于在Laravel中的foreach循环中获取所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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