Laravel图片上传路径异常:需存至public目录却存入storage/app
Hey team mate! Let's get that image upload path sorted out for you.
The issue right now is that your code is using Laravel's default local disk, which saves files to storage/app/. To get your images into the directory you want, here are two straightforward solutions:
Option 1: Use Laravel's built-in public disk (Recommended)
Laravel's default public disk is designed for publicly accessible files, storing them in storage/app/public/ (you can link this to your project's public/storage/ via a symlink for easy web access).
Just modify your controller code to specify the public disk when storing the file:
if ($request->hasFile('image')) { // Add 'public' as the third parameter to use the public disk $request->file('image')->storeAs('images', $filename, 'public'); }
Then run this Artisan command to create the symlink so your files are web-accessible:
php artisan storage:link
After this, your images will live in storage/app/public/images/ and you can access them via your-app-url/storage/images/[filename].
Option 2: Directly save to your project's public/ folder
If you really want files to go straight into my-Project_folder/public/, first add a new disk configuration to config/filesystem.php:
'disks' => [ 'local' => [ 'driver' => 'local', 'root' => storage_path('app'), ], 'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'url' => env('APP_URL').'/storage', 'visibility' => 'public', ], // Add this new disk 'public_root' => [ 'driver' => 'local', 'root' => public_path(), 'visibility' => 'public', ], ]
Then update your controller to use this new disk:
if ($request->hasFile('image')) { $request->file('image')->storeAs('images', $filename, 'public_root'); }
This will save your images directly to public/images/, accessible via your-app-url/images/[filename].
Quick note: Make sure the target directory (either storage/app/public/images or public/images) has proper write permissions for your server, otherwise the upload will fail.
内容的提问来源于stack exchange,提问作者timah

