skip to Main Content

my hosting provider has disabled the symlink() function for security reasons, the Laravel default path to uploading files is in the storage folder, so I need to change the upload path from storage directory to public directory in my Cpanel public_html folder structure like this:

- files:
  -- Laravel framework files and folders such as:
     --- app
     .
     .
     .
     --- storage:
         ---- app:
              ----- public:
                    ------ uploads
- assets:
   -- CSS files and Javascript files and static images
- .htaccess 
- favicon.ico
- index.php
- robot.txt
- web.config

I want to be like this (I mean when a file upload it’s will be stored in the store in public_html => uploads folder):

- files:
  -- Laravel framework files and folders such as:
     --- app
     .
     .
     .
     --- storage:
         ---- app:
              ----- public
- assets:
   -- CSS files and Javascript files and static images
- uploads:
  --- user uploaded files
- .htaccess 
- favicon.ico
- index.php
- robot.txt
- web.config

I add this code in configfilesystem.php in disks array:

'uploads' => [
            'driver' => 'local',
            'root'   => public_path(), // previously storage_path();
        ],

and in my VideoController the upload method code is:

public function uploadVideo (Request $request) {
        $target = $request['target'];

        //Get filename with the extension
        $fileNameWithExt = $request->file($target)->getClientOriginalName();

        //$fileNameWithExt = $request->file('cover_image')->getClientOriginalImage();
        //Get just file name
        $filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
        //Get just ext
        $extension = $request->file($target)->getClientOriginalExtension();
        //Filename to store
        $fileNameToStore = $filename.'_'.time().'.'.$extension;
        //Upload Image
        $path = $request->file($target)->storeAs('public/uploads/' . $target, $fileNameToStore);
        
        // I add this code to create uploads folder in pucli_html
        Storage::disk('uploads')->put('filename', $filename);

        return 'storage/uploads/'. $target .'/'.$fileNameToStore;
    }

but it creates a public folder inside the files folder

2

Answers


  1. create a new storage disk in config/filesystems.php:

    'uploads' => [
        'driver' => 'local',
        'root'   => public_path(),
    ],
    

    replace the FILESYSTEM_DRIVER value in .env with new disk name(uploads)

    and you can upload the file in easy way

    $file = $request->file->hashName();
    $request->file->store('uploads');
    
    Login or Signup to reply.
  2. You don’t need to change any filesystems.php. You upload your video or image anywhere in your project like below

    $upload_path = base_path('../');
    
    $path = $request->file($target)->move(
    
         $upload_path.'/uploads/', $fileNameToStore
    
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search