Laravel Framework Upload Picture to Seven Cow Function Detailed Explanation

  • 2021-12-21 04:26:25
  • OfStack

In this paper, an example is given to describe the function of uploading pictures to 7 cows realized by Laravel framework. Share it for your reference, as follows:

New project

Here directly with a new Laravel 5.3 project for demonstration, other versions are 1 will not affect the function, at most is the routing location is not 1. Small partners of existing projects can skip the second paragraph directly.


#  New project 
laravel new laravel-qiniu
cd laravel-qiniu

Install Laravel7 Bull Expansion Pack

After Composer installation:


composer require zgldh/qiniu-laravel-storage

Then register the service provider in config/app. php:


zgldh\QiniuStorage\QiniuFilesystemServiceProvider::class

Next, add a 7-cow configuration to disks in config/filesystems. php:


'qiniu' => [
  'driver' => 'qiniu',
  'domains' => [
    'default'  => 'xxxxx', // Yours 7 Cow domain name 
    'https'   => 'xxxxx',     // Yours HTTPS Domain name 
    'custom'  => 'xxxxx',   // Your custom domain name 
   ],
  'access_key'=> '', //AccessKey
  'secret_key'=> '', //SecretKey
  'bucket'  => '', //Bucket Name 
  'notify_url'=> '', // Persistence processing callback address 
],

OK, the installation of the expansion package is introduced here for the time being. Next, we will go to 7 Niu to register an account and improve the above configuration.

7 cow account registration and configuration

Go to 7 Niu to register an account first. Clicking on the registration in official website will let us choose the user type. Here I will choose the individual user.

Next, according to the process, the input registration will be OK, so I won't show it to you. Since my mobile phone number has been registered, I can only show you 1 example of the storage object I have added.

OK, simply show you the default domain name of 7 cows and where to see the custom domain name. Let's show you the location of the key of 7 cows:

Click on the secret key management, and you can see the secret key of 7 cows:

Configuration of 7 cattle in Laravel

Where the relevant configurations are already described above, we will now use these configurations in Laravel:

Upload pictures to 7 cows

Simple with an example to demonstrate the front-end upload pictures to the background, with the extension of 7 cows upload pictures

Create a new index. blade. php view under resources\ views


<!DOCTYPE html>
<html>
<head>
  <title> Upload pictures </title>
</head>
<body>
  <form method="post" action="" enctype="multipart/form-data">
    <input type="file" name="file">
    <button type="submit"> Upload pictures </button>
  </form>
</body>
</html>

The page code is simple and has no style (please forgive me for being lazy). Create a new UplaodController upload file controller:


php artisan make:controller UploadController

Implement the upload method:


<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use zgldh\QiniuStorage\QiniuStorage;
class UploadController extends Controller
{
  /**
   *  Upload files to 7 Cattle 
   * @author  Gao Wei 
   * @date  2016-11-09T16:58:37+0800
   * @param Request         $request [description]
   * @return [type]              [description]
   */
  public function uploadFile(Request $request)
  {
    //  Determine whether a file is uploaded 
    if ($request->hasFile('file')) {
      //  Get a file ,file Corresponding to the front-end form upload input Adj. name
      $file = $request->file('file');
      // Laravel5.3 More in the middle 1 A writing method 
      // $file = $request->file;
      //  Initialization 
      $disk = QiniuStorage::disk('qiniu');
      //  Rename file 
      $fileName = md5($file->getClientOriginalName().time().rand()).'.'.$file->getClientOriginalExtension();
      //  Upload to 7 Cattle 
      $bool = $disk->put('iwanli/image_'.$fileName,file_get_contents($file->getRealPath()));
      //  Judge whether the upload is successful 
      if ($bool) {
        $path = $disk->downloadUrl('iwanli/image_'.$fileName);
        return ' Upload successful, picture url:'.$path;
      }
      return ' Upload failed ';
    }
    return ' No files ';
  }
}

Add a route:


//  Upload page view 
Route::get('/upload',function ()
{
  return view('index');
});
// form Commit to controller route 
Route::post('upload','UploadController@uploadFile');

View page (resources\ views\ index. blade. php):


<!DOCTYPE html>
<html>
<head>
  <title> Upload pictures </title>
</head>
<body>
  <form method="post" action="{{url('upload')}}" enctype="multipart/form-data">
    {{csrf_field()}}
    <input type="file" name="file">
    <button type="submit"> Upload pictures </button>
  </form>
</body>
</html>

OK, refresh the page to see the uploaded url address. Here is just a demonstration of the simplest example, routing definition, view style, and logical layer to deal with everyone in accordance with their own projects on the line

For more readers interested in Laravel related content, please check the topics on this site: "Introduction and Advanced Tutorial of Laravel Framework", "Summary of Excellent Development Framework of php", "Introduction Tutorial of php Object-Oriented Programming", "Introduction Tutorial of php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"

I hope this article is helpful to the PHP programming based on Laravel framework.


Related articles: