Automatically uploads all files under the specified folder to Aliyun OSS storage

Source: Internet
Author: User
Tags php script aliyun

First, the environment

1, Aliyun server

2, php5.0+mysql+tinkphp3.2

3. OSS Stored SDK

Second, the logic

The first step: to Aliyun backstage to get Accesskeyid and Accesskeysecret

Step two: To Aliyun OSS object storage to create storage space bucket; Get the space name of the OSS address and bucket

Step three: Traverse all files under the specified folder and subfolders

Step Fourth: Download the OSS SDK to implement the bucket uploaded to the OSS

The fifth step is to specify a timed task to be executed every 2 seconds (used to automatically upload to the OSS, if it is manual can omit this step);

Third, directory structure

Place the downloaded SDK in the vendor directory.

Four, code

<?php//Home namespace Home\controller;
Use Think\controller; 
        Use oss\core\ossexception;//otherwise ossexception the error class Indexcontroller extends Controller {public Function ossscript () {
            do{//$timing = M (' one_timing ')->where (array (' ID ' =>1))->find ()//control of the loop through the background control of the data in the database
            $timing [' timing '] = 1;
            Ignore_user_abort ()//Turn off the browser, the PHP script can also continue to execute. Set_time_limit (0);//Through Set_time_limit (0) allows the program to execute indefinitely $interval =2;//run every 2 seconds//todo//e Cho time (). "
            <br> "; $path = '. /img '//File location ' ...
            /' within the same sibling directory './' Project $this->traverse ($path);
        Sleep ($interval);
           
            while ($timing [' timing '] = = 1);//When True infinite loop} public function traverse ($path = ') {    Traverse the file below the folder $current _dir = Opendir ($path); Opendir () returns a directory handle, and the failure returns false while (($file = Readdir ($current _dir))!== false) {//readdir () returns one of the Open directory handlesEntry $sub _dir = $path. Directory_separator.    $file; Build subdirectory path if ($file = = '. ' | | $file = = ' ... ')
                {continue; else if (Is_dir ($sub _dir)) {//If it is a directory, recursively//echo ' Mulu '. $file.
                    ':<br> ';
                $this->traverse ($sub _dir); else {//If it is a file, direct output $url = $path. '
                    /'. $file; Echo $url.
                    ' <br> ';
                    Echo substr ($url, 2);d ie;
                $this->uploadoss ($url); }} Public Function Uploadoss ($file _url) {/* * $file _url local file Lu Jin * $file _
        Name file * *///uploaded to Aliyun//echo $file _url;
        echo $file _name;die;
        Vendor (' aliyun.autoload ');
        $accessKeyId = "ltai3hlp6logpx1e";//Go to Aliyun backstage to obtain secret key $accessKeySecret = "ne0drfo3sksiq1prgfig259nc9vp7w";//Go to Aliyun backstage to get the secret key $endpoint = "Oss-cn-shanghai.aliYuncs.com "//Your Aliyun OSS address $ossClient = new \oss\ossclient ($accessKeyId, $accessKeySecret, $endpoint);
        $bucket = "public678"; file upload space in//oss $file = $file _url;//file path, must be local. $object = ' uploads/comimg/'. Date (' y-m-d '). /'. $file _name;//want to save the name of the file $file _url = substr ($file _url, 3);//Remove front '.

        /' Symbol $object =str_replace ("\", "/", $file _url);
            try{$mm = $ossClient->uploadfile ($bucket, $object, $file);
            Print_r ($mm);d ie;
            echo $mm;d ie;
        Upload success, your own code unlink ($file);//Here you can delete uploaded files to the local file. The catch (Ossexception $e) {//upload failed, encoding printf itself ($e->getmessage ().
            "\ n");
        Return }
    }



}
If it is useful to you, please point to praise;

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.