php下载远程文件(图片)的三种方法
2015-11-02 10:13
721 查看
1. 使用file_get_contents 和 file_put_contents 方法下载远程图片:
<?php function download_remote_file($file_url, $save_to) { $content = file_get_contents($file_url); file_put_contents($save_to, $content); } ?>
实例:
<?php download_remote_file('http://www.54ux.com/wp-content/themes/d-simple/img/thumbnail.jpg', realpath("./downloads") . '/file.jpg'); ?>
2.使用php CURL 下载远程图片
<?php function download_remote_file_with_curl($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); } ?>
实例:
<?php download_remote_file_with_curl('http://www.54ux.com/wp-content/themes/d-simple/img/thumbnail.jpg', realpath("./downloads") . '/file.jpg'); ?>
3. 使用 fopen 下载远程图片:
<?php function download_remote_file_with_fopen($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 download_remote_file_with_fopen('http://www.54ux.com/wp-content/themes/d-simple/img/thumbnail.jpg', realpath("./downloads") . '/file.jpg'); ?>
相关文章推荐
- tp论坛 分页(三)
- php图形图像处理
- PHP环境搭建Zend Studio 10.6.2+WampServer2.4
- phpStudy 创建多个站点,绑定域名!
- 使用ThinkPHP框架快速搭建网站
- VSftp配置
- ContentProvider浅谈+实例
- 浅谈PHP中foreach/in_array的使用
- php防止用户重复提交表单
- 2.php 字符串,数组,正则,判断符相关
- thinkphp 如何去除url中的index.php
- php防止用户重复提交表单
- PHP就业情况分析
- 浅谈PHP中foreach/in_array的使用
- PHP中Http协议post请求参数
- php+ajax实现无刷新数据分页的办法
- php获取远程文件内容的函数
- PHP函数引用返使用说明
- PHP自定义XML类实现数组到XML文件的转换
- PHP从数据库中取得数据并放入数组(PDO)