Laravel 5.2 File Upload Feature usage example

Source: Internet
Author: User
Tags button type file upload php file php template


Configuration

File system configuration file in config/filesystems.php file, here we create a new uploads local disk space for storing uploaded files, specific configuration items and descriptions as follows:

<?php

return [

/*
|--------------------------------------------------------------------------
| Default filesystem Disk
|--------------------------------------------------------------------------
|
| Here/May specify the default filesystem disk that should to be used
| By the framework. A "local" driver, as a variety of cloud
| Based drivers are available for your choosing. Just Store away!
|
| Supported: "Local", "FTP", "S3", "Rackspace"
|
*/

The default use of local-side space supports "locals", "ftp", "S3", "Rackspace"
' Default ' => ' local ',

/*
|--------------------------------------------------------------------------
| Default Cloud filesystem Disk
|--------------------------------------------------------------------------
|
| Many applications store files both locally and in the cloud. For this
| Reason, you could specify a default "cloud" Driver here. This driver
| would be bound as the Cloud disk implementation in the container.
|
*/

Cloud storage using Amazon S3
' Cloud ' => ' S3 ',

/*
|--------------------------------------------------------------------------
| FileSystem disks
|--------------------------------------------------------------------------
|
| This is configure as many filesystem "disks" as you wish, and
| May even configure multiple disks of the same driver. Defaults have
| been setup for each driver as a example of the required options.
|
*/

' Disks ' => [

Local-side 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) for storing uploaded files
' Uploads ' => [

' Driver ' => ' local ',

Files will be uploaded to the storage/app/uploads directory
' Root ' => storage_path (' app/uploads '),

Files will be uploaded to the public/uploads directory if the browser requires direct access please set this
' Root ' => public_path (' uploads '),
],

Amazon S3 Related Configuration
' S3 ' => [
' Driver ' => ' S3 ',
' Key ' => ' Your-key ',
' Secret ' => ' Your-secret ',
' Region ' => ' your-region ',
' Bucket ' => ' your-bucket ',
],

],

];

Third, code implementation file upload

1. Controller code

<?php

namespace App\http\controllers;

Use Illuminate\http\request;
Use Storage;
Use app\http\requests;


Class Filecontroller extends Controller
{

File Upload Method
Public function upload (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 formerly known as
                $ext = $ File->getclientoriginalextension ();    //EXTENSION
                 $realPath = $file->getrealpath ();  // Absolute path of temporary file
                $type = $ File->getclientmimetype ();    //Image/jpeg

Uploading files
$filename = Date (' Y-m-d-h-i-s '). '-' . Uniqid (). '.' . $ext;
Use our new uploads local storage space (directory)
$bool = Storage::d ISK (' uploads ')->put ($filename, file_get_contents ($realPath));
Var_dump ($bool);

}

}

Return view (' upload ');
}
}

2. The most basic upload.blade.php template code:

<form method= "POST" enctype= "Multipart/form-data" >   
    <input type = "File" name= "picture"
    <button type= "Submit" > Submit </button>
</form>
When you get to a file, you can perform various processing of the file. If it is a picture, you can do a variety of scaling and cropping operations.

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.