Laravel电商应用中如何将文件存储到用户自定义文件夹?
Hey there, let's tackle this file upload issue step by step. That FileNotFoundException you're hitting typically comes from either a missing directory structure or incorrect path handling when interacting with Laravel's filesystem. Here's how to get your files saved to app/public/uploads/{user_id}/attach/ properly:
1. Verify Your Filesystem Configuration
First, make sure your config/filesystems.php has the public disk set up correctly—this is the disk we'll use since we want files to be publicly accessible:
'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'url' => env('APP_URL').'/storage', 'visibility' => 'public', ],
This tells Laravel the public disk's root is storage/app/public, which maps to the public/storage symlink we'll set up later.
2. Implement the User-Specific Upload Logic
In your controller, handle the file upload by first creating the user's directory (if it doesn't exist), then storing the file. Here's a complete example:
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Auth; public function uploadFile(Request $request) { // Validate the uploaded file first (optional but recommended) $request->validate([ 'file' => 'required|file|max:2048', // Adjust max size as needed ]); // Get the current user's ID (adjust this if you're using a different way to fetch the user) $userId = Auth::id(); // Define the target path relative to the public disk's root $targetDir = "uploads/{$userId}/attach"; // Create the directory if it doesn't exist (true creates parent directories too) if (!Storage::disk('public')->exists($targetDir)) { Storage::disk('public')->makeDirectory($targetDir, 0755, true); } // Store the file if ($request->hasFile('file')) { $file = $request->file('file'); // Use store() to save the file—this automatically generates a unique filename $filePath = $file->store($targetDir, 'public'); // If you need to save the path to your database, use $filePath // Example: $model->file_path = $filePath; $model->save(); // To get the public URL for the file, use: // $publicUrl = Storage::url($filePath); } return back()->with('success', 'File uploaded successfully!'); }
3. Fix the FileNotFoundException
Your original error (File not found at path: app/Test1.docx) happened because you were referencing a path that Laravel's filesystem couldn't resolve correctly. By using Storage::disk('public') methods, you let Laravel handle the path resolution relative to the disk's root, eliminating this issue.
4. Set Up the Public Symlink
To make uploaded files accessible via a web URL, run this Artisan command:
php artisan storage:link
This creates a symlink from public/storage to storage/app/public, so you can access files using URLs like https://your-app.com/storage/uploads/1/attach/your-file.docx.
5. Check Permissions
Ensure your storage directory and the public/storage symlink have proper read/write permissions. On most servers, setting storage to 755 and files to 644 works:
chmod -R 755 storage/ chmod -R 644 storage/app/public/
内容的提问来源于stack exchange,提问作者user3617088

