0

So I tried file uploading using Input but sadly failed. I am trying to use Request to do it but I get the follow error:

Call to a member function move() on string

blade:

<input type="file" name="productImage[]" id="" multiple>

controller:

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' => 'Продукт добавен успешно!']);
}

I need to get my files and their names and upload them to my images folder.

Codearts
  • 2,816
  • 6
  • 32
  • 54

3 Answers3

1

Your $productImage is a string, not a file.

$files = Input::file('productImage');
foreach($files as $file) {
    $file->move(...);
    ....
Felippe Duarte
  • 14,901
  • 2
  • 25
  • 29
0

You could use Felippe Duarte's solution or get all files uploaded with the Request as well, calling to file method like this:

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);
}
Jose Rojas
  • 3,490
  • 3
  • 26
  • 40
-1

Try using move_uploaded_file instead of move.

Try checking out this link : PHP - Move a file into a different folder on the server

It is explained more detailed.

Community
  • 1
  • 1
Carlos
  • 1,261
  • 11
  • 27