Laravel 5.1 Example of how files are stored, moved, and deleted

Source: Internet
Author: User
Tags file upload ftp http request save file port number

Laravel based on Flysystem provides a powerful file system for storing and deleting files, which, like caching, supports a variety of drivers, including local drivers, FTP, Amazon S3, and Rackspace, On top of these drives, a unified API is provided to facilitate switching to the driver at any time without modifying any business logic code.

Now that the API approach is consistent, here as an example, we use a local driver to demonstrate how to store and delete files using the file system APIs.

1, configuration

The file system configuration is located in config/filesystems.php, and the default configuration is as follows:

The code is as follows Copy Code

return [

' Default ' => ' local ',
' Cloud ' => ' S3 ',
' Disks ' => [

' 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 ',
],

],

];

As you can see from the configuration file, the default file system driver for Laravel is local, or locally driven, and the default cloud storage is Amazon S3. We can modify these default configurations.

All supported driver and driver detailed configuration definitions in the disks configuration entry, use the root configuration in the local driver to specify the root path of the filesystem, that is, Storage/app, which means that if you use local drivers, all files will be stored in the directory. If you use FTP drivers, you need to specify an FTP host, username, and login password, as well as additional configuration such as port number, timeout, encryption, and so on. Similarly, use S3 or Rackspace to fill in the appropriate configuration items.

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

2. Basic use

We'll use the file system to demonstrate how to upload files, get files, and delete files, and we'll use the method provided by the storage façade.

Uploading files/Getting files

Here we are based on the HTTP request Instance Tutorial # Upload file This tutorial and modify its Upload method Postfileupload:

The code is as follows Copy Code

Public Function Postfileupload (Request $request) {
Determine if the request contains a name=file upload file
if (! $request->hasfile (' file ')) {
Exit (' Upload file is empty! ');
}
$file = $request->file (' file ');
To determine if the file upload process error
if (! $file->isvalid ()) {
Exit (' Error on file! ');
}
$newFileName = MD5 (Time (). Rand (0,10000)). $file->getclientoriginalextension ();
$savePath = ' test/'. $newFileName;
$bytes = Storage::p ut (
$savePath,
File_get_contents ($file->getrealpath ())
);
if (! Storage::exists ($savePath)) {
Exit (' Save file failed! ');
}
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 to indicate that the upload failed, otherwise display upload file content.

For example, we test upload pictures, visit http://laravel.app:8000/request/fileupload, click "Select File" Upload a picture

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.