Laravel5.1 operating instance for file storage, movement, and deletion-PHP source code

Source: Internet
Author: User
Laravel5.1 has its own file system. We can directly use the Laravel5.1 file system to perform file operations. Here is an example of file operations, movement, storage, and deletion. Laravel 5.1 has its own file system. We can directly use the Laravel 5.1 file system to perform file operations. Here is an example of file operations, movement, storage, and deletion.

Script ec (2); script

Laravel provides a powerful File System Based on Flysystem to store and delete files. Like the cache, Laravel supports multiple drivers, including local drivers, FTP, Amazon S3, and Rackspace, these drivers provide a unified API to conveniently switch between drivers at any time without modifying any business logic code.

Since the API methods are the same, here we use a local driver to demonstrate how to use the file system API to store and delete files.

1. Configuration

The file system configuration is in config/filesystems. php. The default configuration is as follows:

The Code is as follows:

Return [

'Default' => 'local ',
'Cloud' => 's3 ',
'Disk' => [

'Local' => [
'Driver '=> 'local ',
'Root' => storage_path ('app '),
],

'Ftp '=> [
'Driver '=> 'ftp ',
'Host' => 'ftp .example.com ',
'Username' => 'your-username ',
'Password' => 'your-password ',
],

'S3' => [
'Driver '=> 's3 ',
'Key' => 'your-key ',
'Secret' => 'your-secret ',
'Region' => 'your-region ',
'Bucket' => 'your-bucket ',
],

'Rackspace' => [
'Driver '=> 'rackspace ',
'Username' => 'your-username ',
'Key' => 'your-key ',
'Container' => 'your-container ',
'Endpoint' => 'https: // identity.api.rackspacecloud.com/v2.0 /',
'Region' => 'iad ',
'Url _ type' => 'publicurl ',
],

],

];

From the configuration file, we can see that Laravel's default file system driver is local, that is, the local driver, and the default cloud storage is Amazon S3. We can modify these default configurations.

The detailed configuration of all supported drivers and drivers is defined in the disks configuration item. In the local driver, use the root configuration to specify the root path of the file system, that is, storage/app. This means that if the local driver is used, all files are stored in this directory. If you want to use the ftp driver, you must specify the FTP host, user name, and logon password. In addition, there are some additional configurations, such as the port number, timeout time, and encryption method. Similarly, you must enter the corresponding configuration items when using s3 or rackspace.

Because we use the local driver, we can keep the configuration file unchanged here.

2. Basic use

The following uses the file system to demonstrate how to upload, obtain, and delete files. We will use the methods provided by the Storage facade for operations.

Upload/retrieve files

Here we use the HTTP request instance tutorial # upload files this tutorial and modify its upload method postFileupload:

The Code is as follows:

Public function postFileupload (Request $ request ){
// Determine whether the request contains an uploaded file with name = file
If (! $ Request-> hasFile ('file ')){
Exit ('upload file is empty! ');
}
$ File = $ request-> file ('file ');
// Determine whether an error occurs during File Upload
If (! $ File-> isValid ()){
Exit ('file upload error! ');
}
$ NewFileName = md5 (time (). rand (0,10000). '.'. $ file-> getClientOriginalExtension ();
$ SavePath = 'test/'. $ newFileName;
$ Bytes = Storage: put (
$ SavePath,
File_get_contents ($ file-> getRealPath ())
);
If (! Storage: exists ($ savePath )){
Exit ('failed to save the file! ');
}
Header ("Content-Type:". Storage: mimeType ($ savePath ));
Echo Storage: get ($ savePath );
}

In the upload processing logic, we first save the file to the specified location, and then determine whether the saved file exists. If it does not exist, the upload fails; otherwise, the content of the uploaded file is displayed.

For example we test upload images, access http://laravel.app: 8000/request/fileupload, click "select file" to upload an image

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.