PHP-based implementation of seven KN crawl remote picture _php instance

Source: Internet
Author: User
Because the company's website before the user picture is stored on their own server, and then feel that management is not convenient, the new added user avatar has been uploaded to seven of cattle, in order to facilitate unified management, the leader said the local server's avatar all migrated to seven cattle.

1. Sort out the ideas

First of all to determine whether the user's avatar in seven cattle, if not present, if there is a local crawl to seven cattle, and then batch crawl

2. Seven bulls determine if the picture exists

/**    * See if the seven KN URL exists    * @param string $url *   /function url_exists ($url) {     require_once (common_path. ") Qiniu/rs.php ");     Require_once (Common_path. " Qiniu/http.php ");     $parts =parse_url ($url);     $bucket;//Bucket     $key =substr ($parts [' path '],);//seven KN filename    //Key     $accessKey;      $secretKey;     Qiniu_setkeys ($accessKey, $secretKey);     $client = new qiniu_machttpclient (null);     List ($ret, $err) = Qiniu_rs_stat ($client, $bucket, $key);//view individual file information     if ($err!== null) {       return false;     } E LSE {       return true;     }   }

3. Move a single file to seven KN

/**    * Move a single avatar to seven kn    * @param string $remotePath The physical address of the remote file on the server    * @param string $qiniuPath Seven kn file address   */ function Onefetch ($remotePath, $qiniuPath) {     if (! $this->url_exists ($qiniuPath) && file_exists ($ RemotePath)) {       require_once (common_path. ") Qiniu/rs.php ");       Require_once (Common_path. " Qiniu/http.php ");       Require_once (Common_path. " Qiniu/conf.php ");       $client = new Qiniu_machttpclient (NULL);       /* $ACCESSURL remote file access address       * $bucket Bucket       * $key stored in seven kn filename       *       /$ret = Qiniu_rs_fetch ($client, $ACCESSURL, $ buckets, $key);       if ($ret! = NULL)       {         $errlog = ' fail.log ';//write error log         file_put_contents ($errlog,  "{$key}\n", File_ APPEND);       }       else       {         var_dump ($ret);       }     } else{       var_dump (' already have Avatar ');     }   }

4. Batch Crawl

function T () {    header ("content-type:text/html; Charset=utf-8 ");    Set_time_limit (0);    $start = intval ($_get[' num ');//start position    $str = ';    /* Log Query from database */    $link = mysql_connect ("localhost", "root", "root") or Die ("error");    $db _selected = mysql_select_db ("Test", $link);    mysql_query ("SET NAMES UTF8");    $limit = 10;//The number of queries per query     $sql = "SELECT * from test ORDER BY id desc limit {$start},{$limit}";      $result = mysql_query ($sql) or Die (Mysql_error ());    if (mysql_num_rows ($result))    {while      ($row =mysql_fetch_assoc ($result)) {          onefetch ($row [' RemotePath '], $row [' Qiniupath ']);      }        $start + = ten;      $url = "/t?num={$start}";      $str = ' Updated '. $start. ' Bar
'; $str. = ";//update $limit bar, jump to prevent script not responding } else { echo ' end '; } echo $str; Ob_flush (); Flush (); Sleep (2); Unset ($sql, $result, $row); }

The above content is based on PHP to achieve seven of cattle crawl remote images of all the content, I hope you like.

  • Related Article

    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.