Laravel 5 Tutorial-file Upload

Source: Internet
Author: User
Tags php template

first, Introduction

Laravel has a great file system abstraction layer, which is based on the Flysystem extension package of Frank de Jonge. The Laravel integrated Flysystem provides a simple interface to operate on-premises, Amazon S3, Rackspace Cloud Storage. More conveniently, It can be very simple to switch between different save methods, but still use the same API operation!

Local-side space is used by Default. of course, You can also set up multiple sets of disks and even use the same driver on multiple disks. The Laravel file system provides a very powerful feature, but this article only describes the commonly used file upload Functionality.

This article introduces using the Local-side space to describe the use of file uploads in Laravel.

second, the configuration

File system configuration file in the config/filesystems.php file, here we create a new uploads local disk space to store uploaded files, specific configuration items and descriptions are as Follows:

<?PHPreturn [    /*    |--------------------------------------------------------------------------    |    Default Filesystem Disk |--------------------------------------------------------------------------| | Here's Specify the default filesystem disk that should be used | By the Framework. A "local" driver, as well as a variety of cloud | Based drivers is available for your choosing.    Just Store away!    | |    Supported: "local", "ftp", "s3", "rackspace" | */    //The default uses Local-side space to support "local", "ftp", "s3", "rackspace"' Default ' = ' local ',/*    |--------------------------------------------------------------------------    |    Default Cloud Filesystem Disk |--------------------------------------------------------------------------| | Many applications store files both locally and in the Cloud. For this | reason, Specify a default "cloud" driver Here. This driver |    'll is bound as the Cloud disk implementation in the Container.    | */    //Cloud storage uses Amazon S3' Cloud ' = ' S3 ',/*    |--------------------------------------------------------------------------    |    Filesystem Disks |--------------------------------------------------------------------------| | Here's Configure as many filesystem "disks" as you wish, and you | May even configure multiple disks of the same driver. Defaults has |    been setup for each driver as an example of the required Options.    | */' Disks ' = [        //Local on-premises Space' Local ' = [            ' Driver ' = ' local ', ' root ' + storage_path (' app '),        ],//public space on the local side' Public ' = [            ' Driver ' = ' local ', ' root ' = ' Storage_path ' (' app/public '), ' visibility ' = ' public ',        ],//Create a new local-side uploads space (directory) to store uploaded files' Uploads ' = [            ' Driver ' = ' local ',//files will be uploaded to the storage/app/uploads directory' Root ' = Storage_path (' app/uploads '),//the file will be uploaded to the Public/uploads directory if you need direct access to the browser please set this//' root ' = Public_path (' uploads '),],//Amazon S3 Related configurations' S3 ' = [            ' Driver ' = ' s3 ', ' key ' = ' Your-key ', ' secret ' = ' Your-secret ', ' region ' = ' Your-region ', ' bucket ' = ' your-bucket ',        ],    ],];
third, the code implementation file Upload 1. Controller code
<?phpnamespace app\http\controllers; useilluminate\http\request; useapp\http\requests; useilluminate\support\facades\storage;classFilecontrollerextendscontroller{//File Upload Method     public functionUpload (Request$request)    {        if($request->ismethod (' Post ')) {            $file=$request-file(' picture '); //whether the file was uploaded successfully            if($file-IsValid ()) {                //Get file-related Information                $originalName=$file->getclientoriginalname ();//file originally                $ext=$file->getclientoriginalextension ();//Extended Name                $realPath=$file->getrealpath ();//absolute path to temporary files                $type=$file->getclientmimetype ();//image/jpeg//uploading Files                $filename=Date(' y-m-d-h-i-s '). ‘-‘ .uniqid() . ‘.‘ .$ext; //use our new uploads local storage (directory)//uploads here is the name of the configuration file                $bool= Storage::d ISK (' uploads ')->put ($filename,file_get_contents($realPath)); Var_dump($bool); }        }        returnView (' upload '); }}    
2-1.upload.blade.php Template Code (upload component is Bootstrap-fileinput) if it's too messy, you can look at the simplest page below:
<! DOCTYPE html>
<link rel= "stylesheet"  href= "/static/layui-v2.1.7/css/layui.css"/>
<script src= "/static/layui-v2.1.7/layui.js" ></script>
<link rel= "stylesheet"href= "/static/css/bootstrap-4.0.0-beta.2/css/bootstrap.min.css"/><script src= "/static/js/jquery/ Jquery-3.2.1.min.js "></script><script src="/static/js/popper/popper.min.js "></script>< Script src= "/static/css/bootstrap-4.0.0-beta.2/js/bootstrap.min.js" ></script>
<link rel= "stylesheet"href= "/static/css/index.css"/><link href= "/static/bootstrap-fileinput/css/fileinput.css" media= "all" rel= " Stylesheet "type=" text/css "/><link href="/static/css/font-awesome-4.7.0/css/font-awesome.min.css "media=" All "rel=" stylesheet "type=" text/css "/><link href="/static/bootstrap-fileinput/themes/explorer-fa/theme.css "media=" all "rel=" stylesheet "type=" text/css "/><script src="/static/bootstrap-fileinput/js/plugins/ Sortable.js "type=" text/javascript "></script><script src="/static/bootstrap-fileinput/js/ Fileinput.js "type=" text/javascript "></script><script src="/static/bootstrap-fileinput/js/locales/ Zh.js "type=" text/javascript "></script><script src="/static/bootstrap-fileinput/themes/explorer-fa/ Theme.js "type=" text/javascript "></script><script src="/static/bootstrap-fileinput/themes/fa/ Theme.js "type=" text/javascript "></script> <title> report upload </title>class= "layui-body" > <div style= "padding:15px;" > <blockquoteclass= "layui-elem-quote" >Report Upload</blockquote> </div> <divclass= "container" > <divclass= "container kv-main" > <form enctype= "multipart/form-data" method= "post" > <labelclass= "control-label" >selectFile</label> <input id= "input-b5" name= "input-b5" type= "file" multiple>{{csrf_field ()}}</form> </div> </div></div></body><script>$ (document). Ready (function(){ $("#input-b5").fileinput ({showcaption:false,Theme: ' FA ',language: ' Zh ',Uploadurl: './upload ',allowedfileextensions: [' jpg ', ' png ', ' gif '] }); }); $.Ajaxsetup ({headers: { ' X-csrf-token ': $ (' meta[name= ' csrf-token "] '). attr (' content ') } });</script>

2-2. the most basic upload.blade.php template code:
<formMethod= "post"enctype= "multipart/form-data" >        <inputtype= "file"name= "picture">    <Buttontype= "submit">Submit</Button></form>

Laravel 5 Tutorial-file upload

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.