PHP downloads remote images to local through url_PHP tutorial

WBOY
Release: 2016-07-13 16:57:22
Original
983 people have browsed it

This article provides a paragraph about saving the image to this address through the image address from an ajax instance of mine. Here we read it directly through readfile and then save it through fopen

Example

/**
* Download to local through the remote url of the image
* @param: $url is the remote link to the image
* @param: $filename is the file name saved after downloading the image
​*/
function GrabImage($url,$filename) {
if($url==""):return false;endif;

ob_start();
readfile($url);
$img = ob_get_contents();
ob_end_clean();
$size = strlen($img);

//"../../images/books/" is the storage directory, $filename is the file name
$fp2=@fopen("../../images/books/".$filename, "a");
fwrite($fp2,$img);
fclose($fp2);

return $filename;
}
?>
The code is as follows
 代码如下 复制代码

/**
* 通过图片的远程url,下载到本地
* @param: $url为图片远程链接
* @param: $filename为下载图片后保存的文件名
*/
function GrabImage($url,$filename) {
if($url==""):return false;endif;

ob_start();
readfile($url);
$img = ob_get_contents();
ob_end_clean();
$size = strlen($img);

//"../../images/books/"为存储目录,$filename为文件名
$fp2=@fopen("../../images/books/".$filename, "a");
fwrite($fp2,$img);
fclose($fp2);

return $filename;
}
?>

Copy code

The function returns the name saved after downloading the image, then you only need to store the image directory + image name in the database. http://www.bkjia.com/PHPjc/631524.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/631524.html
TechArticle
This article provides a paragraph from an ajax instance of mine about saving pictures to this address through the picture address, here We can read it directly through readfile and save it through fopen. The example code is as follows...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template