PHP capture remote picture Save local

Source: Internet
Author: User
/** * Download remote picture to local * * @param $url string remote file Address * @param $filename string file name after save (the file name is randomly generated when empty, otherwise the original file name) * @param $fileType  The file type allowed by array * @param the path saved by the $dirName string file (the remainder of the path is automatically generated based on the time system) * @param $type int How to get the file remotely * @return JSON returns the file name, save path  * @author www.phpxs.com */function download_image ($url, $fileName = ", $dirName, $fileType = array (' jpg ', ' gif ', ' PNG '),    $type = 1) {if ($url = = ") {return false;     }//Get file original filename $defaultFileName = basename ($url);    Get file type $suffix = substr (STRRCHR ($url, '. '), 1);    if (!in_array ($suffix, $fileType)) {return false; }//Set file name after save $fileName = $fileName = = "? Time (). Rand (0, 9). '.' .     $suffix: $defaultFileName;        Gets the remote file resource if ($type) {$ch = Curl_init ();        $timeout = 30;        curl_setopt ($ch, Curlopt_url, $url);        curl_setopt ($ch, Curlopt_returntransfer, 1);        curl_setopt ($ch, Curlopt_connecttimeout, $timeout);        $file = curl_exec ($ch); Curl_cloSE ($ch);        } else {Ob_start ();        ReadFile ($url);        $file = Ob_get_contents ();    Ob_end_clean (); }//Set File save path//$dirName = $dirName. '/' . Date (' Y ', Time ()). '/' . Date (' m ', Time ()). '/' .    Date (' d ', Time ()); $dirName = $dirName. '/' .    Date (' Ym ', Time ());    if (!file_exists ($dirName)) {mkdir ($dirName, 0777, true); }//Save file $res = fopen ($dirName. '/' .    $fileName, ' a ');    Fwrite ($res, $file);     Fclose ($res);    Return Array (' fileName ' = = $fileName, ' savedir ' = $dirName);     }/** * Bulk Download the images in the blog to local */public function index () {global $_g;    $blogModel = Model (' blog ', ' blog ');     $list = $blogModel->order (' gid desc ')->limit (Ten)->findpage (); $page = GET_GPC (' page ')?    GET_GPC (' page '): 1;    $totalPages = $list [' TotalPages '];     $page = $page + 1; if ($page > $totalPages) {die (' Update complete!    '); } foreach ($list [' data '] as $val) {$content = $val [' content '];         $excerpt = $val [' excerpt '];         $_g[' iscontentupdate ' = $_g[' isexcerptupdate '] = false; /* Content */$content = Preg_replace_callback ("/src=\" (http:\/\/images\.cnblogs\.com\/cnblogs_com[^\ "]+) \"/", Function (            $matches) {global $_g;             $_g[' iscontentupdate ') = true;             Download remote picture to local $res = Download_image ($matches [1], ' old ', ' d:/php/xampp/htdocs/emlog/content/uploadfile '); Return the downloaded Image URL address return ' src= '/content/uploadfile/'. Date (' Ym ', Time ()). '/' . $res [' FileName '].         '"';         }, $content); /* Summary */$excerpt = Preg_replace_callback ("/src=\" (http:\/\/images\.cnblogs\.com\/cnblogs_com[^\ "]+) \"/", Function (            $matches) {global $_g;             $_g[' isexcerptupdate ') = true;             Download remote picture d to local $res = Download_image ($matches [1], ' old ', ' d:/php/xampp/htdocs/emlog/content/uploadfile '); Return the downloaded Image URL address return ' src= '/content/uploadfile/' . Date (' Ym ', Time ()). '/' . $res [' FileName '].         '"';         }, $EXCERPT);        /* Update Database */$where = array (' gid ' = $val [' gid ']);         $data = Array ();        if ($_g[' iscontentupdate ') {$data [' content '] = $content;        } if ($_g[' isexcerptupdate ') {$data [' excerpt '] = $excerpt;        if ($data) {$blogModel->where ($where)->save ($data);    }/* Update next page */$url = URL (' blog/main/index ', Array (' page ' = ' $page)); $msg = ' updating '. $page. '/' .    $totalPages; Redirect ($url, 2, $msg);}
  • 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.