php-laravelHow do I upload a file using PHP Laravel?
The following steps explain how to upload a file using PHP Laravel:
- Create a form in your view file with an input type of file.
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="upload_file" />
<input type="submit" value="Upload File" />
</form>
- Create a route in your routes file to handle the form submission.
Route::post('/upload', function() {
// Handle the form submission
});
- Inside the route, you can use the
request()helper to get the file from the form submission.
$file = request()->file('upload_file');
- Use the
store()method to store the file in the desired location.
$file->store('uploads');
- You can also use the
storeAs()method to store the file with a custom name.
$file->storeAs('uploads', 'custom_name.jpg');
- Finally, you can also use the
move()method to move the file to a different location.
$file->move('/path/to/destination', 'custom_name.jpg');
- You can also use the
validate()method to validate the uploaded file before storing it.
$this->validate(request(), [
'upload_file' => 'required|mimes:jpeg,jpg,png|max:2048'
]);
Helpful links
More of Php Laravel
- How do I set up a Laravel worker using PHP?
- How do I set up a .gitlab-ci.yml file for a Laravel project using PHP?
- How do I decide between using PHP Laravel and Yii for my software development project?
- How do I create a controller in Laravel using PHP?
- How can I generate a PDF from HTML using Laravel and PHP?
- How can I use the PHP Zipstream library in a Laravel project?
- How can I use the @yield directive in PHP Laravel?
- How can I use XAMPP to develop a project in Laravel with PHP?
- How do I run a seeder in Laravel using PHP?
- How do I generate a QR code using Laravel and PHP?
See more codes...