Laravel Uploading Files

Retrieving Uploaded Files

We can access files from an Illuminate\Http\Request. By using the file method or using the dynamic properties.

The file method returns an instance of the Illuminate\Http\UploadedFile class that extends the PHP SplFileInfo class. It provides a several methods for interacting with the file

$file = $request->file('photo');
$file = $request->photo; 

We can specify if a file is present on the request by using the hasFile method

if ($request->hasFile('photo')) {
 //statement
 } 

Validating Successful Uploads

To check if the file is present, we have to verify that there was no problem in uploading the file with the help of the isValid method

If ($request->file(‘photo’)->isValid())
 {
 //statement
 } 

File Paths & Extensions

The UploadedFile class contains methods for accessing the file`s which have a fully-qualified path and its extension.

The extension method will try to guess the file`s extension based on its contents.

The extension can be different from the extension which was given by the client.

$path = $request->photo->path();
$extension = $request->photo->extension(); 

Storing Uploaded Files

The UploadedFile class has a store method by which we will move an uploaded file to one of our disks, which can be a location on our local filesystem or even a cloud storage location like Amazon S3.

The store method accepts the path where all the files are stored relative to the filesystem`s configured root directory.

This path does not contain a file name, a unique ID will automatically be generated to serve it as the file name.

The store method also accepts a second argument as an option for the name of the disk which is used to store the file.

The method will return the path of the file relative of the disk`s root.

$path = $request->photo->store(‘images’);
$path = $request->photo->store(‘images’, ‘s3’); 

If we do not want a file name to be generated automatically, then we can use the storeAs method that accepts the path, file name, and the disk name as its arguments

$path = $request->photo->storeAs('images', 'filename.jpg');
$path = $request->photo->storeAs('images', 'filename.jpg', 's3'); 


Related Topics

Laravel Forms

CSRF Field For defining an HTML form in our application, we should include a hidden CSRF token field in the form, so that the CSRF protection middleware can validate the request. We use the...

3 minutes read.

How to Install Git on Windows

Git Installation Git is a distributed version control system. It is used for tracking changes in source code during development. The goals include speed, data integrity, non-linear workflows, etc. Git was developed in 2005. It is free...

3 minutes read.

Laravel First Packages

First Packages  Laravel provides the following ready to use packages: Cashier: It was introduced in Laravel 4.2 version, it provides an interface for managing subscription billing service, like handling coupons and generating invoices. SSH: It was...

2 minutes read.

Laravel Uploading Files

Retrieving Uploaded Files We can access files from an Illuminate\Http\Request. By using the file method or using the dynamic properties. The file method returns an instance of the Illuminate\Http\UploadedFile class that extends the...

2 minutes read.

Laravel Templating Inheritance

Introduction: The Blade is a PHP Templating Engine. It grabs the PHP code and makes it easy for us to implement in HTML. The Blade is a simple, and powerful Templating Engine provided by...

4 minutes read.

Creating First Laravel Project

Creating the Laravel Project For creating the Laravel project, we are going to use a “Git”. If you don`t have a “Git” software and also don`t have the knowledge to install it, then click here. Let`s...

1 minute read.

Laravel Tutorial

Laravel Tutorial is a PHP based web-framework; it is used for developing high-end web applications by using significant syntax's. It has a substantial collection of tools and provides application architecture....

5 minutes read.

Laravel Creating Views

Creating Views Views hold the HTML served by our application, and it separates our controller/application logic with the help of presentation logic. Views are stored inside the resources/views directory. A simple view...

2 minutes read.

Laravel Named Routes

Named Routes Named routes allow the suitable generation of URLs or redirects to specific routes. We specify a name for a route by changing the name method onto the route definition: Syntax:  Route::get(‘user/profile’, function () {...

2 minutes read.

Laravel Displaying Data

Display data that is passed to our Blade views by enclosing the variable in curly braces. The following route is given below: Route::get('greeting', function () { return view('welcome', ['name' => 'rafia']); }); We display...

2 minutes read.

Laravel vs Other Frameworks

Laravel is one of the top listing PHP framework. Some of the reasons which make Laravel top listing PHP framework that are as follows: Authorization Technique.Object-Oriented Libraries.Artisan.MVC Support.Security.Database Migration.Database Template Engine. In this Pie Chart distribution, we...

6 minutes read.

Laravel Route Parameters

Required Parameters: We need to capture segments of the URI within our route. We are going to see how to pass parameters through two or many views inside the closure function. Route::get('/home',...

2 minutes read.

Laravel Eloquent Database Relationships

Eloquent Relationships Laravel: Database tables are related to one another. Eloquent manages and work with easy relationships. It supports some different types of relationship, which are as follows: One to oneOne to manyMany...

8 minutes read.

Laravel Advantages

Top 8 Advantages of Laravel The following advantages that Laravel offers, it is based upon designing a web application:- 1. Powerful Authentication: The Laravel PHP framework was developed with a purpose that can help...

2 minutes read.

Database Migrations

Database Migrations in Laravel Introduction Migrations are version control for our database. It allows our team to modify and share the application`s database. Migrations are paired with schema builder to build an application`s database...

2 minutes read.

Laravel Authentication

Authentication is a process of identifying user credentials. Laravel makes implementing authentication very simple. The authentication configuration file is located at config/auth.php that contains various documented options for adjusting the behavior of the authentication...

4 minutes read.

Laravel Control Statements

Blade also provides convenient shortcuts for common control structure in PHP, such as conditional statements and loops. These shortcuts provide a very clean working with PHP structures, while also remain familiar to...

3 minutes read.

Laravel History and Versions

Laravel History Taylor Otwell created the Laravel; it was created as an attempt to provide an advanced alternative to the CodeIgniter framework. The CodeIgniter does not provide several features such as built-in support for...

3 minutes read.

Features of Laravel Framework

Top 8 Laravel Features Laravel has a very rich set of features that makes the speed of web development faster. The following features that serve the Laravel`s key points: Bundles: Bundles provide us a Modular Packaging...

2 minutes read.

Laravel Controller Middleware

Controller Middleware Middleware can be assigned to the controller`s routes in our route files: Route::get(‘profile’, ‘UserController@show’) ->middleware(‘auth’); It is more convenient to specify middleware within our controller`s constructor. Using the middleware method from our controller`s constructor,...

1 minute read.