In this lesson, I will show you how to upload files, get uploaded files, delete files in laravel application.
Link Storage:
php artisan storage:link
File upload form:
<form action="file-upload" method="post" enctype="multipart/form-data">
@csrf
<div>
<input type="file" name="image" id="image">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
Upload file:
public function update(Request $request)
{
$path = $request->file('image')->store('images', 'public');
return $path;
}
Rename uploaded file:
$path = $request->file('image')->storeAs(
'images', $request->user()->id
);
Upload with Storage Facade:
$path = Storage::putFile('images', $request->file('image'));
Upload with a name:
$path = Storage::putFileAs(
'images', $request->file('image'), $request->user()->id
);
Specifying A Disk:
$path = $request->file('image')->store(
'images/'.$request->user()->id, 's3'
);
File Information:
$request->file('image')->getClientOriginalName();
$request->file('image')->extension();
Get file:
$url = Storage::url('filename.jpg');
Delete File:
Storage::delete('file.jpg');
Storage::delete(['file.jpg', 'file2.jpg']);