简体   繁体   中英

Laravel uploaded file not appears in linked “public/storage” folder

When I run command: php artisan storage:link , this creates folder /public/storage .

then I have code, which handles uploaded file:

// get file original name and extension here, then generate file new name $fileNameToStore
// set file path
$path = $request->file('my_file')->storeAs('public/uploaded_imgs', $fileNameToStore);

Code works and uploaded file appears in /storage/app/public/uploaded_imgs/ folder, which is nice, though there is nothing in /public/storage folder.

Why there is not uploaded_imgs folder in /public/storage directory? What I'm doing wrong?

In config/filesystems.php, you could do this... change the root element in public

Note: instead of upload you can use your folder name

'disks' => [
   'public' => [
       'driver' => 'local',
       'root'   => public_path() . '/uploads',
       'url' => env('APP_URL').'/public',
       'visibility' => 'public',
    ]
]

and you can access it by

Storage::disk('public')->put('uploaded_imgs', $request->file('my_file'));

or

'disks' => [
    'local' => [
        'driver' => 'local',
        'root'   => storage_path(),
    ],
    'uploads' => [
        'driver' => 'local',
        'root'   => public_path() . '/uploads',
    ],
]

Then use it:

Storage::disk('uploads')->put('filename', $file_content);

Try this:

Storage::disk('public')->put('uploaded_imgs', $request->file('my_file'));

hopefully it will work.

When you run php artisan storage:link command, Laravel generates a "storage" symlink under "public" folder which directs to /storage/app/public so your code is correct.

There is no public/storage directory, its a symlink which directs to /storage/app/public which is where your uploaded_imgs folder has been generated.

public/storage => /storage/app/public

You can use the following code to upload a file:

$file = $request->file('my_file');
$fileNameWithoutExtension = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);

$path = "public/uploaded_imgs/" + $fileNameWithoutExtension +"."+$file->getClientOriginalExtension();

Storage::disk('local')->put($path, file_get_contents($file), 'public');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM