PHP下载远程图片的几种方法总结

yipeiwu_com5年前PHP代码库

PHP下载远程图片的几种方法总结

本文演示3个从远程URL下载图片,并保存到本地文件中的方法,包括file_get_contents,curl和fopen。

1. 使用file_get_contents

function dlfile($file_url, $save_to)
{
 $content = file_get_contents($file_url);
 file_put_contents($save_to, $content);
}

2.使用CURL

function dlfile($file_url, $save_to)
{
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_POST, 0); 
 curl_setopt($ch,CURLOPT_URL,$file_url); 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
 $file_content = curl_exec($ch);
 curl_close($ch);
 $downloaded_file = fopen($save_to, 'w');
 fwrite($downloaded_file, $file_content);
 fclose($downloaded_file);
}

3.使用fopen

function dlfile($file_url, $save_to)
{
 $in=  fopen($file_url, "rb");
 $out=  fopen($save_to, "wb");
 while ($chunk = fread($in,8192))
 {
 fwrite($out, $chunk, 8192);
 }
 fclose($in);
 fclose($out);
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

PHP下一个非常全面获取图象信息的函数

复制代码 代码如下:<?php function getimageinfo(img) { //img为图象文件绝对路径 img_info = getimagesize(img);...

PHP里面把16进制的图片数据显示在html的img标签上(实现方法)

客户公司的SQLServer2008的一个生产数据库才1年多就高达18G之巨,原来是系统里面的图片直接以16进制字符串的形式存储在数据库的。要用PHP显示在html页面上,我还耗费了不上...

php set_time_limit()函数的使用详解

语法 : void set_time_limit (int seconds)说明 : 设定一个程式所允许执行的秒数,如果到达限制的时间,程式将会传回错误。它预设的限制时间是30秒,max...

PHP实现ASCII码与字符串相互转换的方法

本文实例讲述了PHP实现ASCII码与字符串相互转换的方法。分享给大家供大家参考,具体如下: <?php class ascii { /** * 将ascii...

php array_filter除去数组中的空字符元素

除去数组中的空字符元素 复制代码 代码如下: <?php $str1_array=array('【宜配屋www.yipeiwu.com】','','//www.jb51.net',...