PHP下载远程文件的3种方法以及性能考虑
2014-02-21      0个评论      
收藏    我要投稿

今天在做导出Excel的时候,总是要测试导出的Excel文件,频繁的下载和打开,很麻烦

 
就想着写段代码一气呵成  服务端导出Excel==>下载Excel文件到本地==>并打开的操作。
 
这里摘出PHP下载远端文件的方案,以备忘。其中第3种方法考虑到文件过大时的性能问题。
 
 
 
 
 
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选项),这样就要先创建一个文件指针给它。
 
 
<?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);
?>
 
 
 
 
 
 
 
 
 
 
 
 
 
 

PHP下载文件的几种方案的更多相关文章

  1. 从后端接口下载文件的2种方式:get方式、post方式

    从后端接口下载文件的2种方式 一.get方式 直接使用: location.href='http://www.xxx.com/getFile?params1=xxx&params2=xxxx' ...

  2. 【文件下载】Java下载文件的几种方式

    [文件下载]Java下载文件的几种方式  摘自:https://www.cnblogs.com/sunny3096/p/8204291.html 1.以流的方式下载. public HttpServl ...

  3. Asp.Net 下载文件的几种方式

    asp.net下载文件几种方式 protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方法 ...

  4. php下载文件的一种方式

    <?php ob_start(); // $file_name="cookie.jpg"; $file_name="abc.jpg"; //用以解决中文不 ...

  5. python下载文件的三种方法

    Python开发中时长遇到要下载文件的情况,最常用的方法就是通过Http利用urllib或者urllib2模块. 当然你也可以利用ftplib从ftp站点下载文件.此外Python还提供了另外一种方法 ...

  6. [转载]Java操作Excel文件的两种方案

    微软在桌面系统上的成功,令我们不得不大量使用它的办公产品,如:Word,Excel.时至今日,它的源代码仍然不公开已封锁了我们的进一步应用和开发.在我们实际开发企业办公系统的过程中,常常有客户这样子要 ...

  7. linux 清空文件的几种方案

    之前要清理文件,都是简单粗暴的rm -rf log文件,最近,发现在某些环境下,是不能删除文件本省的,又必须要清理文件的内容信息,经过亲自实验,目测以下的几种方案是可行的,方案如下: 1.采用vi命令 ...

  8. cmd 环境下载文件的几种方法

    今天渗透测试面试提到cmd下载文件  ,自己只写了js和certutil,还有几种常见的方法自己没想起来 这里记录和总结下 . (自己还是太菜太垃圾.) 0x01 certutil certutil ...

  9. Java下载文件的几种方式

    转发自博客园Sunny的文章 1.以流的方式下载 public HttpServletResponse download(String path, HttpServletResponse respon ...

随机推荐

  1. (翻译)Angular 1.3中的验证器管道

    原文地址:VALIDATORS PIPELINE IN ANGULAR 1.3 我们知道在Angular中操作表单是很爽的.因为Angular本身的作用域模型,我们总能在相应的作用域中获取到表单当前的 ...

  2. HDU 1548 A strange lift(BFS)

    Problem Description There is a strange lift.The lift can stop can at every floor as you want, and th ...

  3. OC基础:属性.点语法.KVC 分类: ios学习 OC 2015-06-24 17:24 61人阅读 评论(0) 收藏

    属性:快速生成setter和getter 属性也包括:声明和实现 1.属性的声明写在.h中 格式:@property 数据类型 变量名; 如果实例变量一致的时候,属性的声明可以合并,每一个属性之间使用 ...

  4. 简单shell实现局域网IP扫描

    #!/bin/bash network=$1 time=$(date +%H%M%S) for i in $(seq $2 $3) do ping -c 1 -w 2 $network.$i > ...

  5. PHP连接MYSQL 报错"No such file or directory"

    首先确定是mysql_connect()和mysql_pconnect()的问题,故障现象就是函数返回空,而mysql_error()返回“No such file or directory”. 写个 ...

  6. IOS 获取中英文字符串长度

    //得到中英文混合字符串长度 方法1 - (int)convertToInt:(NSString*)strtemp { int strlength = 0; char* p = (char*)[str ...

  7. Android 引入外部模块编译选择

    /********************************************************************************* * Android 引入外部模块编 ...

  8. POJ:2386 Lake Counting(dfs)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40370   Accepted: 20015 D ...

  9. Complete the Word

    ZS the Coder loves to read the dictionary. He thinks that a word is nice if there exists a substring ...

  10. 转-Hive/Phoenix + Druid + JdbcTemplate 在 Spring Boot 下的整合

    Hive/Phoenix + Druid + JdbcTemplate 在 Spring Boot 下的整合 http://blog.csdn.net/balabalayi/article/detai ...