7. Niu remote image capturing

Source: Internet
Author: User
Qiniu remotely captures pictures. the user's profile picture is stored on its own server before the company's website. after the management is inconvenient, the new user profile picture is uploaded to Qiniu. in order to facilitate management and unify data, the leader said that he would migrate all the pictures on the local server to Qiniu. 1. sort out the ideas and determine whether the user's avatar is in Qiniu. if it does not exist, capture Qiniu locally, and then batch capture 2. qiniu determines whether the image exists 1 ** 2 * check whether the Qiniu url exists 3 * @ Qiniu remote image capturing

The user profile pictures on the company's website are stored on its own server, which is inconvenient to manage. The new profile pictures are uploaded to Qiniu for convenient management and unified data, the leader said that he would migrate all the pictures on the local server to Qiniu.

1. sort out ideas

First, determine whether the user's avatar is in Qiniu. if it does not exist, capture the Qiniu locally and then perform batch capturing.

2. Qiniu determines whether the image exists

1/** 2 * check whether Qiniu url exists 3 * @ param string $ url 4 */5 function url_exists ($ url) {6 require_once (COMMON_PATH. "qiniu/rs. php "); 7 require_once (COMMON_PATH. "qiniu/http. php "); 8 $ parts = parse_url ($ url); 9 $ bucket; // bucket 10 $ key = substr ($ parts ['path'], 1 ); // Qiniu file name 11 // key 12 $ accessKey; 13 $ secretKey; 14 Qiniu_SetKeys ($ accessKey, $ secretKey); 15 $ client = new Qiniu_MacHttpClient (null ); 16 list ($ ret, $ err) = Qiniu _ RS_Stat ($ client, $ bucket, $ key); // view the information of a single file 17 if ($ err! = Null) {18 return false; 19} else {20 return true; 21} 22}

3. move a single file to Qiniu

1/** 2 * move a single avatar to Qiniu 3 * @ param string $ remotePath physical address of the remote file on the server 4 * @ param string $ qiniuPath Qiniu file address 5*/6 function oneFetch ($ remotePath, $ qiniuPath) {7 if (! $ This-> url_exists ($ qiniuPath) & file_exists ($ remotePath) {8 require_once (COMMON_PATH. "qiniu/rs. php "); 9 require_once (COMMON_PATH. "qiniu/http. php "); 10 require_once (COMMON_PATH. "qiniu/conf. php "); 11 12 $ client = new Qiniu_MacHttpClient (NULL ); 13/* $ accessUrl remote file access address 14 * $ bucket 15 * $ key is stored in Qiniu_RS_Fetch ($ client, $ accessUrl, $ bucket, $ key); 18 19 if ($ ret! = NULL) 20 {21 $ errlog = 'fail. log'; // write error log 22 file_put_contents ($ errlog, "{$ key} \ n", FILE_APPEND); 23} 24 else25 {26 var_dump ($ ret ); 27} 28} else {29 var_dump ('existing Avatar '); 30} 31 32}

4. batch capture

Function t () {header ("Content-type: text/html; charset = utf-8"); set_time_limit (0 ); $ start = intval ($ _ GET ['num']); // start position $ str = ''; /* query records from the database */$ link = mysql_connect ("localhost", "root", "root") or die ("error "); $ db_selected = mysql_select_db ("test", $ link); mysql_query ("set names UTF8"); $ limit = 10; // number of queries each time $ SQL = "SELECT * FROM test order by id desc limit {$ start}, {$ limit}"; $ result = mysql_quer Y ($ SQL) or die (mysql_error (); if (mysql_num_rows ($ result) {while ($ row = mysql_fetch_assoc ($ result )) {oneFetch ($ row ['remotepath'], $ row ['qiniupath']);} $ start + = 10; $ url = "/t? Num = {$ start} "; $ str = 'updated '. $ start .'
'; $ Str. = 'script location. href = "'. $ url. '"; script'; // update $ limit, jump to prevent the script from responding} else {echo 'terminal';} echo $ str; ob_flush (); flush (); sleep (2); unset ($ SQL, $ result, $ row );}

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.