php下载远程文件(图片)的三种方法
2015-09-07 18:11
696 查看
采集数据过程经常需要下载远程的图片保存到本地,本文向大家p介绍三种php下载远程图片的方法:
以下载http://www.54ux.com/wp-content/themes/d-simple/img/thumbnail.jpg图片为例:
实例:
实例:
实例:
转载请注明:wordpress教程网 » php下载远程文件(图片)的三种方法(http://www.54ux.com/a-2455.html)
继续浏览有关的文章
以下载http://www.54ux.com/wp-content/themes/d-simple/img/thumbnail.jpg图片为例:
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'); ?>
转载请注明:wordpress教程网 » php下载远程文件(图片)的三种方法(http://www.54ux.com/a-2455.html)
继续浏览有关的文章
相关文章推荐
- php模式设计之 注册树模式
- PHP 实现简单搜索功能
- php模式设计之 工厂模式
- 用php生成静态html页面
- isset()和empty()
- php模式设计之 单例模式
- 【已解决】could not find driver odbc php windows 7
- php操作mysql函数总结
- PHP使用exec命令执行外边程序失败问题
- php生成中文文件时,文件出现乱码
- php深入学习
- 使用 PHPMailer 发邮件
- PHP xcache
- 安装phpssdb扩展:
- PHP彩信接口开发经验及具体开发实现
- yii2之自己做的提示跳转
- yii2 url生成以及图片等的引入
- ThinkPHP通过AJAX返回JSON的两种实现方法
- PHP 数字转汉字函数
- php 递归的生成目录函数