Home > php教程 > php手册 > PHP通过url下载远程图片到本地

PHP通过url下载远程图片到本地

WBOY
Release: 2016-05-25 16:54:38
Original
966 people have browsed it
本文章从我一个ajax实例中提供了段关于通过图片地地址把图片保存到本址,这里我们直接通过readfile读取然后通过fopen保存即可

实例

 代码如下 复制代码

/**
 * 通过图片的远程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;
 }
?>

函数返回图片下载完后保存的名字,那么只需在数据库中存储 图片目录+图片名即可。



Related labels:
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template