Laravel 使用 for 循环上传多张图片
Laravel multiple image uploads using for loop
我处于一种情况,我将不得不根据用户需要上传一些图片。一个用户可能有 1、2 或更多然后 3++ children。所以我在上传他的 children 图片时使用了 for
循环。这是我的表格:
@for($i=1;$i<=$ticket->children_count;$i++)
<div class="form-group">
<label for="">Child {{ $i }} Name:</label>
<input type="text" name="child_name_{{$i}}" value="" required="" class="form-control">
</div>
<div class="form-group">
<label for="">Child {{ $i }} Photo:</label>
<input type="file" name="child_picture_{{$i}}" value="" required="">
</div>
@endfor
我想从后端接收文件,但不知何故我得到的是空文件。
这是控制器内部的 for
循环:
for ($i=1; $i <= $ticket->children_count ; $i++) {
$file = $request->file("child_picture_.$i");
dd($request->child_name_.$i);
}
上面的代码returns只有$i的值。如何正确接收文件?它必须类似于 child_name_1
或 child_name_2
child_picture_1
或 child_picture_3
等
您应该替换以下内容:
dd($request->child_name_.$i);
// php thinks that you are providing two variables:
// $request->child_name_ and $i
收件人:
dd($request->{'child_name_'.$i});
// makes sure php sees the whole part
// as the name of the property
编辑
对于文件,替换:
$file = $request->file("child_picture_.$i");
收件人:
$file = $request->file("child_picture_" . $i);
对不起,但是对于多个文件你应该使用数组(可维护性,可读性),像这样:
@for($i=1;$i<=$ticket->children_count;$i++)
<div class="form-group">
<label for="">Child {{ $i }} Name:</label>
<input type="text" name="child_names[]" value="" required="" class="form-control">
</div>
<div class="form-group">
<label for="">Child {{ $i }} Photo:</label>
<input type="file" name="child_pictures[]" value="" required="">
</div>
@endfor
然后在您的控制器中检查请求是否有这样的文件:
if ($request->hasFile('child_pictures')) {
$files = $request->file('child_pictures');
foreach($files as $file) {
var_dump($file); // dd() stops further executing!
}
}
我处于一种情况,我将不得不根据用户需要上传一些图片。一个用户可能有 1、2 或更多然后 3++ children。所以我在上传他的 children 图片时使用了 for
循环。这是我的表格:
@for($i=1;$i<=$ticket->children_count;$i++)
<div class="form-group">
<label for="">Child {{ $i }} Name:</label>
<input type="text" name="child_name_{{$i}}" value="" required="" class="form-control">
</div>
<div class="form-group">
<label for="">Child {{ $i }} Photo:</label>
<input type="file" name="child_picture_{{$i}}" value="" required="">
</div>
@endfor
我想从后端接收文件,但不知何故我得到的是空文件。
这是控制器内部的 for
循环:
for ($i=1; $i <= $ticket->children_count ; $i++) {
$file = $request->file("child_picture_.$i");
dd($request->child_name_.$i);
}
上面的代码returns只有$i的值。如何正确接收文件?它必须类似于 child_name_1
或 child_name_2
child_picture_1
或 child_picture_3
等
您应该替换以下内容:
dd($request->child_name_.$i);
// php thinks that you are providing two variables:
// $request->child_name_ and $i
收件人:
dd($request->{'child_name_'.$i});
// makes sure php sees the whole part
// as the name of the property
编辑
对于文件,替换:
$file = $request->file("child_picture_.$i");
收件人:
$file = $request->file("child_picture_" . $i);
对不起,但是对于多个文件你应该使用数组(可维护性,可读性),像这样:
@for($i=1;$i<=$ticket->children_count;$i++)
<div class="form-group">
<label for="">Child {{ $i }} Name:</label>
<input type="text" name="child_names[]" value="" required="" class="form-control">
</div>
<div class="form-group">
<label for="">Child {{ $i }} Photo:</label>
<input type="file" name="child_pictures[]" value="" required="">
</div>
@endfor
然后在您的控制器中检查请求是否有这样的文件:
if ($request->hasFile('child_pictures')) {
$files = $request->file('child_pictures');
foreach($files as $file) {
var_dump($file); // dd() stops further executing!
}
}