The installation

developer.qiniu.com/kodo Object storage can be simply defined as a data pool for storing unstructured data such as pictures, audio, and videos. Compared with a host server, it has the advantages of faster read/write speed and easier sharing.

Go to the CMD terminal, switch to the root directory of your project, and run the following command: ComposerrequireQiniu/php-SDK (ii) Traditional installation enter Qiniuyun, download the official PHp-SDK, decompress the downloaded SDK, and put it into the third library file of the project (vendor or extend)Copy the code

Add processing

To introduce the use Qiniu \ Config. use Qiniu\Storage\BucketManager; use Qiniu\Auth; use Qiniu\Storage\UploadManager; publicfunction insert(){
            $data=input('post.');
            $file = $_FILES;

        // The local file path
        $localFilePath = $file['file'] ['tmp_name'];
        // print_r($localFilePath); die();
        // Intercepts the file name extension
        //$suffix = '.jpg';
         $suffix = strtolower(substr($file['file'] ['name'],strpos($file['file'['name'].'. ')));
           // Generate a unique file name, rename (MD5 encryption old file name + second + random number)
        $fileName = md5($file['file'] ['name']) . date('s',time()) . rand(1.9999999);
        $fileName .= $suffix;

        // Upload qiniu cloud business logic
        $accessKey = ' '; // Go to the console key manager to get the AK
        $secretKey = ' ';// Go to the console key manager to get SK
        $auth = new Auth($accessKey, $secretKey);
        // 7 Niuyun bucket name, according to your actual fill in
        $bucket = ' ';
        // Generate an upload Token
        $token = $auth->uploadToken($bucket);
        // Build the UploadManager object
        $uploadMgr = new UploadManager();

        UploadManager putFile UploadManager putFile UploadManager putFile UploadManager
        list($ret, $err) = $uploadMgr->putFile($token, $fileName, $localFilePath);

        // Error message is displayed
        if($err ! =null) {
            // Can be adjusted to error page
           return json([
               'msg'= >'Upload failed'.'code'= >201.'result'= >' '
           ]);
        }

        // Save the path to our own database
        $data['thumbnail_pic_s'] = 'http://qttirgeqg.hn-bkt.clouddn.com/' . $fileName;
        
        Only these two model methods can automatically write timestamps
        $result =News::create($data);

        return json([
            'msg'= >'Upload successful'.'code'= >200.'result'=>$result
        ]);
    }
    <a href=Javascript :if(confirm(' Do you want to delete? '))window.location='http://www.month.com/dele?id={$v.id}&status={$v.status}'"> delete < / a >Copy the code

Three elements of encapsulation

function writeJson($result=null,$msg=null,$errorCode=0){
    $date=[
        'errorCode'=>$errorCode,
        'data'=>$result,
        'msg'=>$msg
    ];
    return json($date);
}
Copy the code

The domain security

RewriteEngine On RewriteCond %{HTTP_REFERER} ! ^$ [NC] RewriteCond %{HTTP_REFERER} ! # month24.com (NC) this is the domain name or IP address can be hotlinking RewriteRule. * \. (JPG | PNG | jpgrar | zip) $https:www.baidu.com [R, NC, L] # if you don't meet can access to the figure of the domain name or IP, I take this stepCopy the code