PHP Gets the remote picture and downloads it to the local

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

Example One, you can automatically identify the picture type and then save the corresponding

The code is as follows Copy Code

/*
* Function: PHP perfect implementation download remote picture save to local
* Parameters: File URL, save file directory, save file name, use the download method
* Use the original name of the remote file when the save file name 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 a Save directory
if (!file_exists ($save _dir) &&!mkdir ($save _dir,0777,true)) {
Return Array (' file_name ' => ', ' Save_path ' => ', ' Error ' =>5);
}
Methods used to get remote files
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 not the picture will automatically skip

The code is as follows Copy Code

<?php
function Grabimage ($url, $filename = "") {
if ($url = = ""): return false;
endif
If the $url address is empty, exit directly
if ($filename = = "") {
If you do not specify a new file name
$ext = STRRCHR ($url, ".");
Get the $url picture format
if ($ext!= ". gif" && $ext!= ". jpg"): return false;
endif
If the picture is not formatted as. gif or. jpg, exit directly
$filename = Date ("Dmyhis"). $ext;
Name the new file name with the day of the month
}
Ob_start ()//Open output
ReadFile ($url);//Output picture file
$img = ob_get_contents ()//Get 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 it
Fclose ($fp 2);
return $filename//Returns a new file name
}
$img = Grabimage ("Http://www.111cn.net/6648d73db0edd1e89f3d62f7.jpg", "");
if ($img): Echo ' <pre></pre> ';
If the return value is true, this shows the picture that has been collected on the server
Else:echo "false";
endif
Otherwise, the output acquisition fails
?>

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

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.