phplaravellaravel-5.1

Laravel 5.1 using storage to move the file to a public folder


In my Laravel API call, I use Storage to upload a file. Then I wold like to rename and move the file to a public directory by using:

    $dest = '/var/www/public/uploads/';
    Storage::disk('local')->put($filename.'.'.$extension,  File::get($file));
    $oldfilename = $filename.'.'.$extension;
    Storage::move($oldfilename, $dest.$newImageName);

But instead of moving the file to the $dest directory, I get a folder created with the $dest dir name inside my storage/app folder and the file is copied there. I have also tried using double quotes around $dest.$newImageName but no success.


Solution

  • If you have an array ['disks']['public'] in config/filesystems.php you can do:

    Storage::disk('public')->move($oldfilename, $dest.$newImageName);
    

    If you don't create one that looks like this:

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

    And then you'll bi able to use the public "disk" with the storage facade.

    Note that you don't have to specify the absolute path anymore, Laravel will know that the storage path is app/public.

    EDIT

    Another config option that might work:

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