所以我尝试使用Input上传文件,但遗憾地失败了。我正在尝试使用Request
来执行此操作,但我收到了以下错误:
调用字符串
上的成员函数move()
刀片:
<input type="file" name="productImage[]" id="" multiple>
控制器:
public function addProduct(Request $request)
{
$this->validate($request, [
'title' => 'required',
'description' => 'required',
'details' => 'required',
'price' => 'required',
'subcategory' => 'required',
'color' => 'min:1',
'size' => 'min:1',
'productImage' => 'min:1'
]);
$product = new Product();
$product->subcategory_id = $request['subcategory'];
$product->gender_id = $request['gender'];
$product->title = $request['title'];
$product->description = $request['description'];
$product->details = $request['details'];
$product->price = $request['price'];
$product->save();
foreach ($request['color'] as $color_id) {
$color = Color::find($color_id);
$product->colors()->save($color);
}
foreach ($request['size'] as $size_id) {
$size = Size::find($size_id);
$product->sizes()->save($size);
}
foreach ($request->file('productImage') as $productImage) {
$file = $productImage;
$file->move('images', $file->getClientOriginalName());
$image = new Image();
$image->product_id = $product->id;
$image->path = $file->getClientOriginalName();
$product->images()->save($image);
}
return redirect()->route('admin.products')->with(['added' => 'Продукт добавен успешно!']);
}
我需要获取我的文件及其名称,然后将其上传到我的图片文件夹。
答案 0 :(得分:1)
你的$ productImage是一个字符串,而不是一个文件。
$files = Input::file('productImage');
foreach($files as $file) {
$file->move(...);
....
答案 1 :(得分:0)
你可以使用Felippe Duarte的解决方案或者使用Request
上传所有文件,调用这样的文件方法:
foreach ($request->file('productImage') as $productImage) {
$file = $productImage;
$file->move('images', $file->getClientOriginalName());
$image = new Image();
$image->product_id = $product->id;
$image->path = $file->getClientOriginalName();
$product->images()->save($image);
}
答案 2 :(得分:-1)
尝试使用move_uploaded_file
代替move
。
尝试查看此链接:PHP - Move a file into a different folder on the server
更详细地解释了。