omar590's avatar

File Upload Permission denied

Hi there I have a weird issue with file permissions.

When I create a product, I upload images with, and it works fine.

But if I tried to upload images in the update function it gives me that error

fopen(C:\xampp\htdocs\IES\ememii\public): failed to open stream: Permission denied

the HTML code

<div class="input-group"> <div class="custom-file col-md-3"> <input type="file" name="img[]" class="custom-file-input " id="exampleInputFile"> <label class="custom-file-label" for="exampleInputFile">Choose Image</label> </div> </div>

and the create function

`$product =new Product();

    $product->name = $request->get('name');

    $product->description = $request->get('description');

    $product->width = $request->get('width');

    $product->weight = $request->get('weight');

    $product->height = $request->get('height');

    $product->length = $request->get('length');

    $product->stock = $request->get('stock');

    $product->price = $request->get('price');

    $product->save();

    foreach ($request->img as $image){
        $name = time().'_'.$image->getClientOriginalName();
        $path = $image->storeAs('products', $name, 'public');
        $photo = new Image();
        $photo->name = $name;
        $photo->link = $path;
        $photo->product_id = $product->id;
        $photo->save();`

the update function

` $product = Product::find($id);

    $product->update($request->all());

    foreach ($request->img as $image){

        $name = time().'_'.$image->getClientOriginalName();
        $path = $image->storeAs('products', $name);
        $photo = new Image();
        $photo->name = $name;
        $photo->link = $path;
        $photo->product_id = $product->id;
        $photo->save();
    }`

does anyone know why?

thank you!

0 likes
2 replies
Wakanda's avatar

@omar590

change from

    $product->update($request->all());

    foreach ($request->img as $image){

        $name = time().'_'.$image->getClientOriginalName();
        $path = $image->storeAs('products', $name);
        $photo = new Image();
        $photo->name = $name;
        $photo->link = $path;
        $photo->product_id = $product->id;
        $photo->save();
    }`

to

   $product->update($request->only(['name', 'description', 'width', 'weight', 'height', 'length', 'stock', 'price']));

  if ($request->img) {
	  foreach ($request->img as $image){

        $name = time().'_'.$image->getClientOriginalName();
        $path = $image->storeAs('products', $name);
        $photo = new Image();
        $photo->name = $name;
        $photo->link = $path;
        $photo->product_id = $product->id;
        $photo->save();
    }
}
1 like
omar590's avatar

Thanks for replying,

yeah that was another issue, I solved it but didn't work

but what I found is that the image itself which causes this problem

I chose another image and it worked just fine

thanks again for replying

1 like

Please or to participate in this conversation.