Waseem's avatar

When using ->put code is creating a directory with the same name as the file.

Hi

I am just trying to upload a file and save it off, however for some reason it is being saved in a directory with the same name as the file.

My code is :

        $file = $request->photo;

        $path = $file->hashName('styles');

        $disk = Storage::disk('public');

        $disk->put($path, $file);




        return $disk->url($path);

My filesystems file under config is as follows


  'disks' => [

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

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

        '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'),
        ],

    ],


so the file will be stored in directory structure like this

QgXDVY6vXR5Ao3xPbBihs5NXIKZFg8uvmUJfrCfx.png
    QgXDVY6vXR5Ao3xPbBihs5NXIKZFg8uvmUJfrCfx.png

But when i do the call to ->url the result will be like this


http://localhost/storage/styles/QgXDVY6vXR5Ao3xPbBihs5NXIKZFg8uvmUJfrCfx.png

As you can see I can never find the file with the path returned and saved off.In addition to this echoing out the paths etc all gives the result without the additional directory

I have tried to work out what it is but at the moment hitting a brick wall

Thanks

0 likes
1 reply
bobbybouwmann's avatar
Level 88

You need to use $request->file('photo') to get the actual file and it's content. After that you can move the file to any location you wish:

$path = $request->file('photo')->store('directory');

// Or
$path = $request->file('photo')->storeAs('directory', 'filename');

// Or
$path = Storage::putFile('directory', $request->file('photo'));

Documentation: https://laravel.com/docs/5.6/filesystem#file-uploads

Please or to participate in this conversation.