Thinkphp Integration series of Aliyun OSS

Source: Internet
Author: User
Tags php source code aliyun

Web2.0 times; In addition to the pure Information Display class website, basically all have the file to upload the function;

The worst thing you have to do is change your avatar, but as business grows;

If the uploaded files are together with the site program source code, there are quite a number of abuses;

1: The static file will occupy a large amount of bandwidth;

2: The cost of the server is slightly higher;

The general practice is to put the PHP source code on a server, pictures and other static files on another server;

When a magical "cloud" era arrives, everything becomes simpler;

When the business is still relatively small, we do not have to bother to engage in a static file server, directly using a third party can;

Well, Lori long-winded; below please the protagonist of this blog: Aliyun oss;

I no longer have the advantage of Amway OSS; I don't know. Follow this link first: https://www.aliyun.com/product/oss/

One: Preparation work

1: Apply for key and secret

2: Create bucket

Attention should be paid to read and write access to: public reading;


Get the extranet endpoint;https://help.aliyun.com/document_detail/31834.html based on the location of the server

OK; These configuration items are gathered; The dragon is summoned below;

Two: Fill in the configuration item

Still be with Project example: Http://git.oschina.net/shuaibai123/thinkphp-bjyadmin

/application/common/conf/config.php

' Alioss_config ' => array (
' key_id ' => ',//Aliyun oss key_id
' Key_secret '              => ',//Aliyun oss key_secret
' end_point ' => ',//Aliyun OSS endpoint
' BUCKET ' => '//Bucken name
),

Three: Instantiate and upload

/application/common/common/function.php

/**  *  instantiation Aliyun oos  *  @return  object  instantiated Objects  */Function new_oss () { 
   vendor (' alioss.autoload ');
     $config =c (' alioss_config ');      $oss =new \oss\ossclient ($config [' key_id '], $config [' Key_secret '], $config [' End_
Point ']);
    return  $oss; /**  *  upload files to OSS and delete local files  *  @param   string  $path   file path  *  @return  bollear       whether to upload  */function oss_upload ($path) {     //  Get Configuration Items      $bucket =c (' Alioss_config.
BUCKET ');
    //  first to remove the left. or/  Add./     $oss _path=ltrim ($path, './');
     $path = './'. $oss _path;     if  (File_exists ($path))  {        //   Instantiation of OSS class          $oss =new_oss ();         //  Upload to oss         
     $oss->uploadfile ($bucket, $oss _path, $path);         //  if required to upload to OSS   automatically delete local files   Then delete the following note    
       // unlink ($path);
        return true;
    }     return false;
 }


OK now first upload the file to the server using the frame's upload upload class, get the file path;

Call the Oss_upload () function, the incoming file path, and upload the file to the OSS;

Four: Convenient Way

Look at the third step; we can find that every upload of a file will call the Oss_upload () function;

That can upload the file automatically uploaded to the OSS.

Sure, but we have to do some surgery on the upload of the frame;

Add the following code to line No. 229 of/thinkphp/library/think/upload.class.php;

Add part start *************************//  determine if upload success if  (!empty ($info))  { 
   //  get directories that need to be uploaded to OSS      $need _upload_oos=c (' Need_upload_oss ');     foreach  ($info  as  $k  =>  $v)  {         //  determine if this path needs to be uploaded to Oss         foreach  ($ need_upload_oos as  $m  =>  $n)  {             if  (Strpos ($v [' Savepath '],  $n)!==false)  {                 //  uploading to OSS      
           oss_upload ($v [' Savepath ']. $v [' savename ']);
                continue;             }         }                     
 &NBSP}}//************************* add partial end *************************

The files that are then set up in the configuration entry are automatically uploaded to the OSS;

/application/common/conf/config.php

' Need_upload_oss ' => Array (///The directory to be uploaded
'/upload/avatar ',
'/upload/cover ', '
/upload/image /webuploader ',
'/upload/video ',
),

If only individual eyes do not upload, or all directories are uploaded, directly change configuration items and upload can be;

Four: Get the file link

Static files such as pictures are uploaded to the OSS, and how are the links obtained?

/application/common/common/function.php

/**
* Get full network connection
* @param string $path file path
* @return string http connection
/function Get_url ($path    {
//if NULL; Returns an empty
if (empty ($path)) {return
';
//If there is already HTTP directly returned if
(Strpos ($path, ' http://')!==false) {return
$path;
//Get bucket
$bucket =c (' Alioss_config.    BUCKET ');
Return ' http://'. $bucket. Oss-cn-beijing.aliyuncs.com '. $path;
}

Call the above Get_url () function, pass the file path to the third step, and access the OSS file link;

To this, from creating, uploading, getting file links, the whole process is over;

If you want to integrate into your own projects;

It only needs to copy the/thinkphp/library/vendor/alioss folder to its own project and the directory;

The above configuration items and functions to take the past can be directly used;

This article for Bai Jun Remote original article, reproduced without contact with me, but please specify from Bai Jun remote blog http://baijunyao.com

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.