W3docs

How to upload files in Laravel directly into public folder?

To upload files directly into the public folder in Laravel, you can use the storeAs method provided by the Storage facade.

To upload files directly into the public disk in Laravel, you can use the storeAs method provided by the Storage facade. Note that Laravel's public disk points to storage/app/public, not the web root public directory.

You can use the following code snippet to upload a file:

Example of uploading a file in Laravel

$path = $request->file('file')->storeAs('public', 'your_file_name.ext');

This will store the uploaded file in the storage/app/public directory with the name your_file_name.ext. To make it accessible via the URL /storage/your_file_name.ext, you must first run php artisan storage:link to create a symbolic link from public/storage to storage/app/public.

Alternatively, you can move the file directly to the public/uploads directory using the move method, skipping the intermediate storage step.

Example of moving the file to the public folder in Laravel

$fileName = $request->file('file')->getClientOriginalName();
$request->file('file')->move(public_path('uploads'), $fileName);

This will store the file into the public/uploads directory. For security best practices, always validate uploaded files before processing them (e.g., request()->validate(['file' => 'required|file|mimes:jpg,png,pdf|max:2048']);).