Laravel storage outside on localmachine in folder like as 'C:/uploads'

3.3k Views Asked by At

I want to save file in folder outside Laravel project to folder on local machine for example 'C:/uploads'. When the project installed on local machine is saved in 'C:/uploads' folder but when the project is installed online on server don't save in 'C:/uploads'.

In filesystems.php I put like as:

'local' => [
     'driver' => 'local',
      //'root' => storage_path('app/public'),
      //'root' => public_path('uploads'),
      'root'=>'C:\uploads',
 ],

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

In HomeController.php function:

$files = Input::file('images');
$file_count = count($files);
$uploadcount = 0;        
foreach($files as $file) {
  //$destinationPath = 'uploads';
  $filename = time() .'-'.$file->getClientOriginalName();
  //$file->move($destinationPath, $filename);
  Storage::disk('local')->put($filename, File::get($file));
  $image=new Image();
  $image->sparepart_id = $id;
  $image->Filename=$filename;
  $image->save();
  $uploadcount ++;

}

1

There are 1 best solutions below

2
On

You can use Laravel's environment variables to achieve this.

In your .env file (example 1)

FILESYSTEM_DRIVER=name_of_your_disk

Or define the application environment (example 2)

APP_ENV=production

Then change these values accordingly for your local env and server env and use them in your code to check what env is being used.

In your home controller you can simply use the Storage facade without the disk call to use the default disk defined in ex 1. Or you could check for APP_ENV and save or not save to the desired disk based on that.

if(env('APP_ENV') !== 'production')
{
    Storage::disk('local')->put($filename, File::get($file))
}