gpt4 book ai didi

php - Laravel:使用干预在 foreach 循环中上传带有图像的表单数据

转载 作者:可可西里 更新时间:2023-11-01 13:20:46 25 4
gpt4 key购买 nike

我正在使用 Laravel 5.1 并希望有一个包含几行文本字段及其相应图像上传的表单。

表格:

<div id="row">
<input name="description[]" type="text">
<input name="image[]" type="file">
</div>
<div id="row">
<input name="description[]" type="text">
<input name="image[]" type="file">
</div>

Controller :

foreach($request->description as $key => $val){

if($val != null){
$data = new Finding;
$data->description = $val; //save description for each loop

$file = array('image' => Input::file('image'));

if (Input::file('image')->isValid()) {

$destinationPath = 'uploads';
$extension = Input::file('image')->getClientOriginalExtension();
$fileName = rand(1000,1000).'.'.$extension;
Input::file('image')->move($destinationPath, $fileName);
$path = Input::file('image')->getRealPath();

$data->image_location = $fileName[$key]; //save filename location to db
$data->save();

flash('success', 'Uploaded Successful');
return Redirect::to('/upload');
} else {
flash('error', 'Uploaded File Is Not Valid');
return Redirect::to('/upload');
}
}

我的问题是,如何使用带有 $key 值的干预来保存表格中的新行以及与图像上传相关联的文本描述,并且仍然使用所有干预类?我的代码关闭了吗?

我可以轻松地完成所有这些,只需一个表单输入和一张图片上传,但我的目标是拥有一个包含多行输入的页面。谢谢!

最佳答案

我不确定您是如何管理多个字段的,是一成不变的还是可以动态更改的。我以前使用 javascript 完成动态方法,并跟踪我需要多少个动态字段以及一个隐藏的文本字段作为计数。

使用这种方法,您的 html 可能看起来像这样:

<input name="imageAndDescriptionCount" type="hidden" value="2">
<div id="row">
<input name="description[]" type="text">
<input name="image[]" type="file">
</div>
<div id="row">
<input name="description[]" type="text">
<input name="image[]" type="file">
</div>

所以这里我们有两个图像和描述字段,计数器设置为 2。

如果我们通过 dd() 提交表单并检查请求,它将看起来像这样:

"imageAndDescriptionCount" => "2"
"description" => array:2 [▼
0 => "description"
1 => "description"
]
"image" => array:2 [▼
0 => UploadedFile {#154 ▶}
1 => UploadedFile {#155 ▶}
]

然后这会很好地为 Controller 中的 for 循环做好准备:

$count = $request->get('imageAndDescriptionCount');
$description = $request->get('description'); // assign array of descriptions
$image = $request->file('image'); // assign array of images

// set upload path using https://laravel.com/docs/5.1/helpers#method-storage-path
// make sure 'storage/uploads' exists first
$destinationPath = storage_path . '/uploads';

for($i = 0; $i < $count; $i++) {

$data = new Finding;
$data->description = [$i];

$file = $image[$i];

if ($file->isValid()) {
$extension = $file->getClientOriginalExtension(); // file extension
$fileName = uniqid(). '.' .$extension; // file name with extension
$file->move($destinationPath, $fileName); // move file to our uploads path

$data->image_location = $fileName;
// or you could say $destinationPath . '/' . $fileName
$data->save();
} else {
// handle error here
}

}

flash('success', 'Uploads Successful');
return Redirect::to('/upload');

请注意,据我所知,您的代码中没有使用干预库,我刚刚编写了执行上传的代码。我希望这可以帮助你!

编辑

如果您真的想使用 foreach 循环并且您知道每个图像都有描述,您可以这样做(但我个人认为使用计数器是更好的方法)

$descriptions = $request->get('description'); // assign array of descriptions
$images = $request->file('image'); // assign array of images

// loop through the descriptions array
foreach($descriptions as $key => $val) {

// You can access the description like this
$val
// or this
$descriptions[$key]

// so naturally you can access the image like this:
$images[$key]

}

您也可以改为检查描述/图像的计数,并将其用作循环遍历的计数器:

$descriptions = $request->get('description');
$images = $request->file('image'); // assign array of images

$descCount = count($descriptions);
$imgCount = count($images);

但是,如果您可能有不同的布局,即您没有对 1 张图片的 1 条描述,那么请说明,因为您需要采用不同的方法。

关于php - Laravel:使用干预在 foreach 循环中上传带有图像的表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36015885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com