gpt4 book ai didi

php - Laravel 5.2 中的多图像上传

转载 作者:可可西里 更新时间:2023-11-01 12:45:24 25 4
gpt4 key购买 nike

我终于可以上传和移动图片了,但现在我想在 Laravel 上创建多个上传图片。那可能吗?我必须使用数组来制作它吗?

我可以从这段代码中稍微修改一下吗?

它在我的 ProductController.php 上

$picture = '';

if ($request->hasFile('images')) {
$file = $request->file('images');
$filename = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
$picture = date('His').$filename;
$destinationPath = base_path() . '\public\images/';
$request->file('images')->move($destinationPath, $picture);
}

if (!empty($product['images'])) {
$product['images'] = $picture;
} else {
unset($product['images']);
}

谢谢。注:我上面的代码来自stackoverflow上的好心人,再次感谢;)

最佳答案

在您的前端表单中,您必须使用像

这样的字段属性名称
name="images[]"

你的 Controller 代码应该是这样的。

$picture = '';
if ($request->hasFile('images')) {
$files = $request->file('images');
foreach($files as $file){
$filename = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
$picture = date('His').$filename;
$destinationPath = base_path() . '\public\images';
$file->move($destinationPath, $picture);
}
}

if (!empty($product['images'])) {
$product['images'] = $picture;
} else {
unset($product['images']);
}

关于php - Laravel 5.2 中的多图像上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36832358/

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