PHP Gets the remote picture and downloads the saved to the local method analysis _php Tips

Source: Internet
Author: User
Tags file size file url readfile save file strlen

This article is an example of how PHP gets a remote picture and downloads the method saved to the local. Share to everyone for your reference, specific as follows:

Remote picture refers to the data on the remote server we can read the download through many functions in PHP, and here we have sorted out two examples of downloading remote images and downloading them locally.

Example 1, you can automatically recognize a picture type and then save it accordingly

* * Function: php perfect download remote picture save to Local * parameter: file URL, save file directory, save file name, use download method * Use the remote file name when saving the filename is empty * * function getImage ($url, $save _ Dir= ', $filename = ', $type =0) {if (trim ($url) = = ') {return array (' file_name ' => ', ' Save_path ' => '), ' ERROR ' =>
 1);
 ' If (Trim ($save _dir) = = ') {$save _dir= './';
    } if (Trim ($filename) = = ") {//save filename $ext =strrchr ($url, '. ');
 if ($ext!= '. gif ' && $ext!= '. jpg ') {return array (' file_name ' => ', ' Save_path ' => ' ", ' Error ' =>3);
  $filename =time (). $ext;
 } if (0!==strrpos ($save _dir, '/')) {$save _dir.= '/'; //Create the Save directory if (!file_exists ($save _dir) &&!mkdir ($save _dir,0777,true)) {return array (' file_name ' => '), ' Save
 _path ' => ', ' Error ' =>5);
 ///Get remote files by the method if ($type) {$ch =curl_init ();
 $timeout = 5;
 curl_setopt ($ch, Curlopt_url, $url);
 curl_setopt ($ch, curlopt_returntransfer,1);
 curl_setopt ($ch, Curlopt_connecttimeout, $timeout);
 $img =curl_exec ($ch);
  Curl_close ($ch);
   }else{Ob_start ();
   ReadFile ($url);
   $img =ob_get_contents (); OB_end_clean ();
  }//$size =strlen ($IMG);
  File size $fp 2= @fopen ($save _dir. $filename, ' a ');
  Fwrite ($fp 2, $img);
 Fclose ($fp 2);
  Unset ($img, $url);
Return Array (' file_name ' => $filename, ' Save_path ' => $save _dir. $filename, ' error ' =>0);

 }

Example 2, if the picture is not automatically skipped

<?php
function Grabimage ($url, $filename = "") {
 if ($url = = ""): return false;
 endif;
 If the $url address is empty, simply exit if
 ($filename = = "") {
 //If no new filename is specified
 $ext = STRRCHR ($url, ".");
 Get $url Picture Format
 if ($ext!= ". gif" && $ext!= ". jpg"): return false;
 endif;
 If the picture is not formatted as. gif or. jpg, exit
 $filename = Date ("Dmyhis") directly. $ext;
 Name the new filename on the day of the month (
 ob_start);//Open Output
 ReadFile ($url);//Output picture file
 $img = ob_get_contents ();// Get the browser output
 Ob_end_clean ()//clear output and close
 $size = strlen ($img);//Get picture size
 $fp 2 = @fopen ($filename, "a");
 Fwrite ($fp 2, $img);//write a picture file to the current directory and rename
 fclose ($fp 2);
 return $filename//returns a new filename
}
$img = Grabimage ("Http://www.jb51.net/6648d73db0edd1e89f3d62f7.jpg", "");
if ($img): Echo ' <pre></pre> ';
If the return value is true, this shows that the picture that has been collected on the server
Else:echo "false";
endif;
Otherwise, the output acquisition fails
?>

In fact, the above download way small series favorite is not the two but Curl save asynchronous download pictures will be better.

For more information on PHP related content readers can view the site topics: "PHP graphics and pictures Operating skills summary", "Basic PHP Grammar Introductory Course", "PHP Operations and Operator Usage Summary", "PHP object-oriented Program Design Introductory Course", "PHP Network Programming Skills Summary", "PHP array (Array) Operations tips Encyclopedia, "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP Common database Operation tips Summary"

I hope this article will help you with the PHP program design.

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.