PHP file upload Qiniuyun

PHP

  Laravel uses qiniu/php-sdk, itbdw/laravel-storage-qiniu, components to upload files to Qiniu Cloud.

 

I. Preamble

 

I have been using qiniu on the laravel framework in the past few days, and found that there are few such tutorials, so I wrote down my own experience for your reference.

There are two components that can be used here, one is the PHP general qiniu/php-sdk, a general component that can be used as long as you use PHP,

And itbdw/laravel-storage-qiniu is a file upload component whose author optimizes the qiniu/php-sdk component according to the characteristics of laravel to facilitate the use of laravel. Thanks to them!

 

2. Preamble

 

Environment: php:7.0.12, laravel.

 

1) First, create a small module.

 

a) link address

admin/test/fileupload

 

b) Routing

Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function() {

  Route::get('test/fileupload', "TestController@fileUpload");

  Route::post('test/upload', "TestController@upload");

});

 

c) The controller presents the view

  TestController/fileUpload

 

d) view

admin/test/fileupload

 

  The view only needs a form, plus input:file, that's all.

 

Tip : To upload files, you need to change the form encoding, here is enctype. You must use multipart/form-data when uploading files.

<form action="{{url('admin/test/upload')}}" method="post" enctype="multipart/form-data">

 

 

 

 

 

.enctype: Specifies how the form data should be encoded before sending it. There are three setup types

Defaults to application/x-www-form-urlencoded: encode all characters before sending

.multipart/form-data: No character encoding, this value must be used when the form contains the file upload control

.text/plain: spaces are converted to "+" plus signs, but special characters are not encoded.

 

e) processing.

 

Select an image here and click upload, the form form is submitted to the TestController/upload controller. dd print it.

 

 

 

 

 

  See the upload file information, and now start uploading the Qiniu cloud operation.

 

3. Installation

 

1) composer download qiniu/php-sdk, itbdw/laravel-storage-qiniu two components.

 

Both of these components can be found on https://packagist.org/ . In fact, each component also has its own tutorial, but it is not aimed at.

 

2) Open the command line and cd to the laravel folder

 

cd ../laravel

(cd your own address here)

 

3) 安装itbdw/laravel-storage-qiniu, qiniu/php-sdk

 

composer require itbdw/laravel-storage-qiniu

 

Tip : There will be an error during the installation process.

 

 

 

The installation request of doctrine/instantiator (locked to 1.1.0) -> can be satisfied by doctrine/instantiator [1.1.0], this component requires PHP ^7.1 version, so my version does not meet the requirements. At this time, install doctrine/instantiator namely Yes, it can also be found on Packagist, the function is: used to instantiate objects in PHP without calling the constructor.

 

composer require doctrine/instantiator

 

 

  You will find that he gives you the backward version. At this time, you can install the itbdw/laravel-storage-qiniu component again.

 

composer require itbdw/laravel-storage-qiniu

 

 

  After the installation is successful, qiniu/php-sdk will be installed automatically, and it is found that there are two folders of itbdw and qiniu under laravel/vendor.

 

 

 

4. Use

 

a) itbdw/laravel-storage-qiniu

 

1. Add a line to the providers array in config/app.php

itbdw\QiniuStorage\QiniuFilesystemServiceProvider::class,

 

 

Tip : This is equivalent to registering middleware, telling the framework that I have registered a new component in the provider.

 

2. Add the disks array in config/filesystem.php :

 

'disks' => [

 

        // If there are multiple buckets, add similar configuration

        'qiniu' => [

            'driver' => 'qiniu',

            'domain' => 'https://www.example.com', //your Qiniu domain name, supports http and https, or without protocol, default http

            'access_key' => '',                       //AccessKey

            'secret_key' => '',                       //SecretKey

            'bucket' => '', //Bucket name

        ],

       

        'qiniu_private' => [

            'driver' => 'qiniu',

            'domain' => 'https://www.example.com', //your Qiniu domain name, supports http and https, or without protocol, default http

            'access_key'    => '',                         //AccessKey

            'secret_key' => '',                            //SecretKey

            'bucket' => 'qiniu_private',                 //Bucket名字

        ],

       

        ...

],

 

Tip : There are two arrays in the disks array. You can see the names. One is for normal use and the other is private. I only added a general one, which is the first one. If you want to keep it secret, you can use the second one. indivual.

 

. Let's talk about these parameters here.

 

'driver' # I won't talk about it, the default is qiniu

 

'domain' => 'https://www.example.com',  

// Your Qiniu domain name, (support http and https, or no protocol, default http)

 

 

'access_key' # public key

'secret_key' # secret

 

 

 

'bucket' # bucket is the name of your space

 

  

3. The use of itbdw/laravel-storage-qiniu does not need to introduce a namespace, it can be used directly. There are also two methods in its readme, but they can be used in combination.

 

Code :

$disk = \Storage::disk('qiniu'); // Upload using Qiniu Cloud 

$time     = date('Y/m/d'); 

$filename = $disk->put($time, $request->file('file')); // 上传 

$img_url = $disk->downloadUrl($filename); // Get the download link

 

 

 

Print the obtained download address, and you can see that the upload has been successful.

  

 

  The image address prefixed with your domain name and time is generated, so that you can write it into the database in the form of a string, and directly <img src=”” when referencing, that’s it.

 

4. qiniu/php-sdk

 

. Introduce

 

  In the controller, use

 

use Qiniu\Storage\UploadManager;

use Qiniu\Auth;

 

 

in the method

 

 

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

// laravel gets the information of the file, if it is normal PHP, use $_FILES['file'].

      

       $tmpPath = $file->getRealPath(); // Get the absolute path of the image in the local

       $ext      = $file->getClientOriginalExtension();   

// get suffix name

      

$fileName = time() . rand(1000, 10000) . '.' . $ext; 

// concatenate filename

      

      

       $accessKey = 'AccessKey'; # public key

       $secretKey = 'SecretKey'; # private key

        $upManager = new UploadManager();

        $auth    = new Auth($accessKey, $secretKey);

        $token = $auth->uploadToken($bucketName); # Upload space name

        list($ret, $error) = $upManager->putFile($token, $fileName, $tmpPath);

 

Tip : The document uses put, but I tried to use put unsuccessfully, so I changed it to putFile.

 

// Pass in the parameters in turn, print them after uploading, and see the effect is successful.

      

       if ($error !== null) {

          dd($error);

       } else {

          dd ($ ret);

       }

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324821002&siteId=291194637