Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

armancs's avatar

Can't Move uploaded file to public folder Laravel 6

I am trying to move image to public folder but its not store to that specific folder.

$data = new Product();
   $data->fill($request->all());
   $data->created_by_id = Auth::user()->id;
   if($file = $request->file('product_image'))
   {
       $image_name = time() . '.' . $file->getClientOriginalExtension();
       $destinationPath = public_path('images\product');
       $file->move($destinationPath,$image_name);
       $data['product_image'] = $image_name;
   }
   $data->save();
   return \redirect()->back();
0 likes
12 replies
Maria30's avatar

@armancs Typo in your code:

$destinationPath = public_path('images\product');

Change it to:

$destinationPath = public_path('images/product');
armancs's avatar

This is my app url

APP_URL=http://localhost
armancs's avatar

also tried like this method but not moving.

$data = new Product();
   $data->fill($request->all());
   $data->created_by_id = Auth::user()->id;
   if($file = $request->hasFile('product_image'))
   {
       $image_name = $file->getClientOriginalName();
       $filename = \pathinfo($image_name,\PATHINFO_FILENAME);
       $image_ext = $file->getClientOriginalExtension();
       $fileNameToStore = $filename.'-'.time().'.'.$image_ext;
       $path = $file->storeAs('public/product_images',$fileNameToStore);
   }
   else{
       $fileNameToStore = 'noimage.jpg';
   }
   $data->product_image = $fileNameToStore;
   $data->save();
   return \redirect()->back();
Snapey's avatar

is your public folder writable by your web server?

Snapey's avatar

this line

if($file = $request->hasFile('product_image'))

I dont think will give you the file. It will only tell you if request has a file

Surprised you are not seeing errors

Maria30's avatar

@armancs do you have any validation?

You need validation, and your code will be processed only if validation passes

Maria30's avatar

@armancs

$photo = new Photo();

$file = $request->file('file');   //file is the name of the html form input
        
               $new_image_name = time().'-'.$file->getClientOriginalName();
               $file->move(public_path().'/img/',$new_image_name); //img is my directory that images go
              $photo->path = $new_image_name; //path is the name of the colomn iin the  database where i save the photo so i can return it to the view
               $photo->save();

armancs's avatar

@snapey Thanks for your replay. I did writable access for public folder and tried this code.

$data = new Product();
    $data->fill($request->all());
    $data->created_by_id = Auth::user()->id;
    if($file = $request->file('product_image'))
    {
        $image_name = time() . '.' . $file->getClientOriginalExtension();
        $destinationPath = public_path('images/product');
        $file->move($destinationPath,$image_name);
        $data['product_image'] = $image_name;
    }
    $data->save();
    return \redirect()->back();

Nothing happen. image name store in database but not moving folder.

also changed config/filesystem.php

'disks' => [

    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],

    'public' => [
        'driver' => 'local',
        'root' => storage_path('/images/product'),
        'url' => env('APP_URL').'/public',
        'visibility' => 'public',
    ],

    's3' => [
        'driver' => 's3',
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION'),
        'bucket' => env('AWS_BUCKET'),
        'url' => env('AWS_URL'),
    ],

],

Public folder have permission to rwe

chmod -R 775
armancs's avatar
armancs
OP
Best Answer
Level 2

Solved this problem.

I forgot to add enctype="multipart/form-data" in my form.

when upload an image or file enctype should be include in form.

and this is my final code for controller.

$this->validate($request,[
    'product_image' => 'mimes:jpeg,jpg,png,gif|required|max:10000'
   ]);
   if($request->hasFile('product_image'))
   {
       $image_name = $request->file('product_image')->getClientOriginalName();
       $filename = pathinfo($image_name,PATHINFO_FILENAME);
       $image_ext = $request->file('product_image')->getClientOriginalExtension();
       $fileNameToStore = $filename.'-'.time().'.'.$image_ext;
       $path =  $request->file('product_image')->storeAs('public/product',$fileNameToStore);
      
   }
   else{
       $fileNameToStore = 'noimage.jpg';
   }
   $data = new Product();
   $data->fill($request->all());
   $data->created_by_id = Auth::user()->id;
   $data->product_image = $fileNameToStore;
   $data->save();
   return \redirect()->back();

And for view:

just added enctype.

                <form class="needs-validation" novalidate method="post" action="{{ route('product.store') }}" enctype="multipart/form-data">

THanks everyone for your effort.

Snapey's avatar

next time, check it step by step. Dont just paste in a load of code and expect it to work.

You question was about moving the file to the folder, which is just about the last step. It does not help if you have not proven ANY of the earlier steps

2 likes

Please or to participate in this conversation.