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

yipeiwu_com6年前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中集成PayPal标准支付的实现方法分享

PHP中集成PayPal标准支付的实现方法分享

PayPal支付功能其实一直在更新文档和接口,这里说的是一个简单的支付功能大概流程如下 1,在网站的结账页面,设置一个提交到PayPal网站的form,里面有一些金额,商品名称,商家收款...

PHP4和PHP5共存于一系统

PHP4和PHP5共存于一系统˂!-- google 的广告条 2005年09月20日换位置 唉,22号被停了.郁闷,没作弊呀 11.27日重开了 ˂!-- googl...

php实现的常见排序算法汇总

本文汇总了常见的php排序算法,在进行算法设计的时候有不错的借鉴价值。现分享给大家供参考之用。具体如下: 一、插入排序 用文字简单的描述,比如说$arr = array(4,2,4,6,...

laravel创建类似ThinPHP中functions.php的全局函数

前言 一直觉得ThinPHP中的公共函数是一个很好的设计,因为我们只需要在functions.php中对共用的函数进行封装,然后就可以在全局直接进行调用了。其实Laravel中也有类似的...

smarty section简介与用法分析

基本原形为: {section name = name loop = $varName[, start = $start, step = $step, max = $max, show...