PHP下载远程文件的3种方法以及性能考虑
今天在做导出Excel的时候,总是要测试导出的Excel文件,频繁的下载和打开,很麻烦
就想着写段代码一气呵成 服务端导出Excel==>下载Excel文件到本地==>并打开的操作。
这里摘出PHP下载远端文件的方案,以备忘。其中第3种方法考虑到文件过大时的性能问题。
原创文章,转载请注明 http://www.cnblogs.com/phpgcs/
3种方案:
-rw-rw-r-- 1 liuyuan liuyuan 470 Feb 20 18:12 test1_fopen.php
-rw-rw-r-- 1 liuyuan liuyuan 541 Feb 20 18:06 test2_curl.php
-rw-rw-r-- 1 liuyuan liuyuan 547 Feb 20 18:12 test3_curl_better.php
方案1,适用于小文件
直接使用fopen()/file_get_contents()获取文件流并用file_put_contents()写入
<?php
//an example xls file form baidu wenku
$url = 'http://bs.baidu.com/wenku4/%2Fe43e6732eba84a316af36c5c67a7c6d6?sign=MBOT:y1jXjmMD4FchJHFHIGN4z:lfZAx1Nrf44aCyD6tJqJ2FhosLY%3D&time=1392893977&response-content-disposition=attachment;%20filename=%22php%BA%AF%CA%FD.xls%22&response-content-type=application%2foctet-stream'; $fp_input = fopen($url, 'r');
file_put_contents('./test.xls', $fp_input); exec("libreoffice ./test.xls", $out, $status);
?>
方案2:通过Curl获取内容
<?php
//an example xls file form baidu wenku
$url = 'http://bs.baidu.com/wenku4/%2Fe43e6732eba84a316af36c5c67a7c6d6?sign=MBOT:y1jXjmMD4FchJHFHIGN4z:lfZAx1Nrf44aCyD6tJqJ2FhosLY%3D&time=1392893977&response-content-disposition=attachment;%20filename=%22php%BA%AF%CA%FD.xls%22&response-content-type=application%2foctet-stream'; $ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
file_put_contents('./test.xls', curl_exec($ch));
curl_close($ch); exec("libreoffice ./test.xls", $out, $status);
?>
第1,2种方案存在一个问题,就是在写入本地磁盘之前,文件会被读入内存中,那么当文件很大的时候,可能会超出内存而崩溃
即使你的内存设置的足够的大,那这也是不别要的开销
解决方法是:直接给CURL一个可写的文件流来让它自己来解决这个问题(通过 CURLOPT_FILE选项),这样就要先创建一个文件指针给它。
原创文章,转载请注明 http://www.cnblogs.com/phpgcs/
<?php
//an example xls file form baidu wenku
$url = 'http://bs.baidu.com/wenku4/%2Fe43e6732eba84a316af36c5c67a7c6d6?sign=MBOT:y1jXjmMD4FchJHFHIGN4z:lfZAx1Nrf44aCyD6tJqJ2FhosLY%3D&time=1392893977&response-content-disposition=attachment;%20filename=%22php%BA%AF%CA%FD.xls%22&response-content-type=application%2foctet-stream'; $fp_output = fopen('./test.xls', 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp_output);
curl_exec($ch);
curl_close($ch); exec("libreoffice ./test.xls", $out, $status);
?>
参考文章:
http://stackoverflow.com/questions/3938534/download-file-to-server-from-url
http://www.phpriot.com/articles/download-with-curl-and-php
扩展阅读
PHP获取远程文件的大小的3种方法
http://wk.yl1001.com/doc_9631373592501870_all.htm
支持断点续传功能
http://www.pureweber.com/article/resumable-download/
PHP下载远程文件的3种方法以及性能考虑的更多相关文章
- php 下载远程图片 的几种方法(转)
1.获取远程文件大小及信息的函数 function getFileSize($url){ $url = parse_url($url); if($fp = @fso ...
- PHP读取远程文件的三种方法
file_get_contents <?php$url = http://www.xxx.com/;$contents = file_get_contents($url);//如果出现中文乱码使 ...
- PHP读取远程文件的4种方法
1. fopen, fread1 if($file = fopen("http://www.example.com/", "r")) {2 while(!feo ...
- 网络编程(一):用C#下载网络文件的2种方法
使用C#下载一个Internet上的文件主要是依靠HttpWebRequest/HttpWebResonse和WebClient.具体处理起来还有同步和异步两种方式,所以我们其实有四种组合. 1.使用 ...
- PHP下载远程图片的几种方法总结
1. 使用file_get_contents function dlfile($file_url, $save_to) { $content = file_get_contents($file_url ...
- 关于下载远程文件为未知文件.txt的解决方法
本地下载文件后缀正常,服务器下载文件后缀都为.txt的解决方法: 后缀为 未知文件.txt 的原因为前端无权限获取Content-Disposition中的文件名 response.setHeader ...
- 合并BIN文件的两种方法(转)
源:http://blog.chinaunix.net/uid-20745340-id-1878803.html 合并BIN文件的两种方法 在单片机的开发过程中,经常需要将两个单独的BIN文件合并成一 ...
- elf格式转换为hex格式文件的两种方法
这周工作终于不太忙了,可以写点笔记总结一下了. 之前的文章如何在Keil-MDK开发环境生成Bin格式文件,介绍了如何在Keil开发环境使用fromelf软件,将生成的axf文件转换为bin文件,这次 ...
- 安卓手机下载YouTube视频的3种方法
作为全球最大的在线视频网站,YouTube上面的内容可真是应有尽有啊,从教学视频到个人手工艺品制作流程,从各种搞笑视频到电视连续集等等,包罗万象.如果你想下载YouTube视频到电脑上面的话,网上有很 ...
随机推荐
- 使用openssl生成密钥、加密和签名
openssl genrsa -out rsakey.pem 1024 //生成1024bit的RSA密钥,并保存到rsakey.pem,此处未对密钥进行加密 openssl genrsa -aes ...
- python之web路径扫描工具
# coding: UTF-8 import sys, os, time, httplibimport relist_http=[] #http数组 def open_httptxt(): #打开 ...
- window7下面安装pear.pchar--wamp环境
准备工作: Wamp php版本:5.3.10 1.下载pear.phar 2.设置php路径的path环境变量 开始安装 1.以管理员身份运行cmd 2.进入pear.phar的所在目录 3.命令行 ...
- 【招聘App】—— React/Nodejs/MongoDB全栈项目:消息列表
前言:最近在学习Redux+react+Router+Nodejs全栈开发高级课程,这里对实践过程作个记录,方便自己和大家翻阅.最终成果github地址:https://github.com/66We ...
- [Functional Programming] Randomly Pull an Item from an Array with the State ADT (Pair)
Functor composition is a powerful concept that arises when we have one Functor nested in another Fun ...
- B2:观察者模式 Observer
定义了一种一对多的依赖关系,多个观察者对象同时监听某一主题的变化,这个主题对象在状态发生变化时,会通知所有观察者对象,使它们可以更新自己. 应用场景:某个实例的变化影响到了其他对象. UML: 示例代 ...
- 在LoadRunner脚本中实现随机ThinkTime
一般情况下,我们都是通过Run-Time Settings来设置Think Time(思考时间),可以设置回放脚本时忽略思考时间,或者是设置回放随机的一段思考时间. By default, when ...
- win10系统怎样手动安装cab更新补丁
win10系统怎样手动安装cab更新补丁 1. 把所有补丁放进一个文件夹 例如 C:\UPDATE2. 以管理员运行命令提示符 3. 输入以下命令後按 Enterdism /online /add-p ...
- lodash forIn forOwn 遍历对象属性
_.forIn(object, [iteratee=_.identity]) 使用 iteratee 遍历对象的自身和继承的可枚举属性. function Foo() { this.a = 1; th ...
- ScrollView反弹效果
public class BounceScrollView extends ScrollView { private View inner;// 孩子View private float y;// 点 ...