一 : PHP通过header实现文本文件的下载
PHP帮助文档里面关于PHP通过header触发下载的说明比较简单,而网上关于此方面的文章也少的可怜,有很多文章都无法实现所需要的效果。今天我也来谈一下这个方面的话题,如果你感觉比网上的某些文章有所改进,那我就很知足了。
如果从准确的角度来说,那PHP文档是最准确的,因为它很简练的列出了实现文本类文件触发下载所需要的三条语句,以PDF为例就是:
以下为引用的内容: // We'll be outputting a PDF header('Content-type: application/pdf'); // It will be called downloaded.pdf header('Content-Disposition: attachment; filename="downloaded.pdf"'); // The PDF source is in original.pdf readfile('original.pdf'); |
这三句是正确的,但是在真正用的过程中很容易出现一些无法预料的问题,如果你是一个很仔细的人的话,也可以很容易的避免这些问题。而我不是,所以我就遇到了这样的问题,这里就以我的问题来简单说一下。
对于第一句,应该没啥说的,是必须的,只要改一下文档的类型就行,例如是下载txt文件,那就改为header(’Content-type: application/txt’);,第二句也没啥说的,就是为你的下载文档起一个名字,如果是txt文件的话,可以改为header(’Content-Disposition: attachment; filename=”downloaded.txt”‘);,第三句的问题就比较多了,readfile这个函数的意思就是读取一个文件然后输出,这里文件的路径需要是真实的文件路径,如果是downloads文件夹下面的一个original.txt文件,可以这样写readfile(’downloads/original.txt’);,而如果提交的页面会输出文本等字符,那么下载到的文件会是原文件original.txt和提交的页面输出的文字的混合文件。我在这里就缺少了仔细的观察,一看下面不对就立即去查代码了,而没发现上面的文本就是我需要的内容,发现了这部分内容,你可能就很快想到怎么来解决这个问题了,也就是关闭提交到的页面的文本内容的输出。 到这里,问题就解决了,从而也就实现了文本文件链接被点击的时候会触发下载对话框的效果。
二 : PHP下载CSS文件中的图片
作为一个资深并且专业的扒皮人员,在我从初三开始投入伟大的互联网中到现在积累了丰富的扒皮经验。我相信每个做web的程序员也都会有类似的经历。
在扒皮过程中,必不可少的需要下载样式文件中的图片。碰到比较庞大的样式文件,其中可能会有上百个需要下载的图片,那么使用下面这段小代码是最为合适的了。
< ?php
/*
More & Original PHP Framwork
Copyright (c) 2007 - 2008 IsMole Inc.
Author: kimi
Documentation: 下载样式文件中的图片,水水专用扒皮工具
*/
//note 设置PHP超时时间
set_time_limit(0);
//note 取得样式文件内容
$styleFileContent = file_get_contents('images/style.css');
//note 匹配出需要下载的URL地址
preg_match_all("/url((.*))/", $styleFileContent, $imagesURLArray);
//note 循环需要下载的地址,逐个下载
$imagesURLArray = array_unique($imagesURLArray[1]);
foreach($imagesURLArray as $imagesURL) {
file_put_contents(basename($imagesURL), file_get_contents($imagesURL));
}
最后预祝各位在扒皮的过程中,一扒到底!
三 : PHP下载远程文件类(支持断点续传)
1.功能:支持断点续传的下载,能计算传输率,能控制传输率
简易使用方法:
以下为引用的内容: $object = new httpdownload(); $object->set_byfile($file)%N#H#%;//服务器文件名,包括路径 $object->filename = $filename;//下载另存为的文件名 $object->download(); 3.源文件: class httpdownload { var $data = null; var $data_len = 0; var $data_mod = 0; var $data_type = 0; var $data_section = 0; //section download var $sentSize=0; var $handler = array('auth' => null); var $use_resume = true; var $use_autoexit = false; var $use_auth = false; var $filename = null; var $mime = null; var $bufsize = 2048; var $seek_start = 0; var $seek_end = -1; var $totalsizeref = 0; var $bandwidth = 0; var $speed = 0; function initialize() { global $HTTP_SERVER_VARS; if ($this->use_auth) //use authentication { if (!$this->_auth()) //no authentication { header('WWW-Authenticate: Basic realm="Please enter your username and password"'); header('HTTP/1.0 401 Unauthorized'); header('status: 401 Unauthorized'); if ($this->use_autoexit) exit(); return false; } } if ($this->mime == null) $this->mime = "application/octet-stream"; //default mime if (isset($_SERVER['HTTP_RANGE']) || isset($HTTP_SERVER_VARS['HTTP_RANGE'])) { if (isset($HTTP_SERVER_VARS['HTTP_RANGE'])) $seek_range = substr($HTTP_SERVER_VARS['HTTP_RANGE'] , strlen('bytes=')); else $seek_range = substr($_SERVER['HTTP_RANGE'] , strlen('bytes=')); $range = explode('-',$seek_range); if ($range[0] > 0) { $this->seek_start = intval($range[0]); } if ($range[1] > 0) $this->seek_end = intval($range[1]); else $this->seek_end = -1; if (!$this->use_resume) { $this->seek_start = 0; //header("HTTP/1.0 404 Bad Request"); //header("Status: 400 Bad Request"); //exit; //return false; } else { $this->data_section = 1; } } else { $this->seek_start = 0; $this->seek_end = -1; } $this->sentSize=0; return true; } function header($size,$seek_start=null,$seek_end=null) { header('Content-type: ' . $this->mime); header('Content-Disposition: attachment; filename="' . $this->filename . '"'); header('Last-Modified: ' . date('D, d M Y H:i:s GMT' , $this->data_mod)); if ($this->data_section && $this->use_resume) { header("HTTP/1.0 206 Partial Content"); header("Status: 206 Partial Content"); header('Accept-Ranges: bytes'); header("Content-Range: bytes $seek_start-$seek_end/$size"); header("Content-Length: " . ($seek_end - $seek_start + 1)); } else { header("Content-Length: $size"); } } function download_ex($size) { if (!$this->initialize()) return false; ignore_user_abort(true); //Use seek end here if ($this->seek_start > ($size - 1)) $this->seek_start = 0; if ($this->seek_end <= 0) $this->seek_end = $size - 1; $this->header($size,$seek,$this->seek_end); $this->data_mod = time(); return true; } function download() { if (!$this->initialize()) return false; try { error_log("begin downloadn", 3,"/usr/local/www/apache22/LOGS/apache22_php.err"); $seek = $this->seek_start; $speed = $this->speed; $bufsize = $this->bufsize; $packet = 1; //do some clean up @ob_end_clean(); $old_status = ignore_user_abort(true); @set_time_limit(0); $this->bandwidth = 0; $size = $this->data_len; if ($this->data_type == 0) //download from a file { $size = filesize($this->data); if ($seek > ($size - 1)) $seek = 0; if ($this->filename == null) $this->filename = basename($this->data); $res = fopen($this->data,'rb'); if ($seek) fseek($res , $seek); if ($this->seek_end < $seek) $this->seek_end = $size - 1; $this->header($size,$seek,$this->seek_end); //always use the last seek $size = $this->seek_end - $seek + 1; while (!(connection_aborted() || connection_status() == 1) && $size > 0) { if ($size < $bufsize) { echo fread($res , $size); $this->bandwidth += $size; $this->sentSize+=$size; } else { echo fread($res , $bufsize); $this->bandwidth += $bufsize; $this->sentSize+=$bufsize; } $size -= $bufsize; flush(); if ($speed > 0 && ($this->bandwidth > $speed*$packet*1024)) { sleep(1); $packet++; } } fclose($res); } elseif ($this->data_type == 1) //download from a string { if ($seek > ($size - 1)) $seek = 0; if ($this->seek_end < $seek) $this->seek_end = $this->data_len - 1; $this->data = substr($this->data , $seek , $this->seek_end - $seek + 1); if ($this->filename == null) $this->filename = time(); $size = strlen($this->data); $this->header($this->data_len,$seek,$this->seek_end); while (!connection_aborted() && $size > 0) { if ($size < $bufsize) { $this->bandwidth += $size; $this->sentSize+=$size; } else { $this->bandwidth += $bufsize; $this->sentSize+=$bufsize; } echo substr($this->data , 0 , $bufsize); $this->data = substr($this->data , $bufsize); $size -= $bufsize; flush(); if ($speed > 0 && ($this->bandwidth > $speed*$packet*1024)) { sleep(1); $packet++; } } } else if ($this->data_type == 2) { //just send a redirect header header('location: ' . $this->data); } if($this->totalsizeref==$this->sentSize )error_log("end downloadn", 3,"/usr/local/www/apache22/LOGS/apache22_php.err"); else error_log("download is canceledn", 3,"/usr/local/www/apache22/LOGS/apache22_php.err"); if ($this->use_autoexit) exit(); //restore old status ignore_user_abort($old_status); set_time_limit(ini_get("max_execution_time")); } catch(Exception $e) { error_log("cancel downloadn".$e, 3,"/usr/local/www/apache22/LOGS/apache22_php.err"); } return true; } function set_byfile($dir) { if (is_readable($dir) && is_file($dir)) { $this->data_len = 0; $this->data = $dir; $this->data_type = 0; $this->data_mod = filemtime($dir); $this->totalsizeref = filesize($dir); return true; } else return false; } function set_bydata($data) { if ($data == '') return false; $this->data = $data; $this->data_len = strlen($data); $this->data_type = 1; $this->data_mod = time(); return true; } function set_byurl($data) { $this->data = $data; $this->data_len = 0; $this->data_type = 2; return true; } function set_lastmodtime($time) { $time = intval($time); if ($time <= 0) $time = time(); $this->data_mod = $time; } function _auth() { if (!isset($_SERVER['PHP_AUTH_USER'])) return false; if (isset($this->handler['auth']) && function_exists($this->handler['auth'])) { return $this->handler['auth']('auth' , $_SERVER['PHP_AUTH_USER'],$_SERVER['PHP_AUTH_PW']); } else return true; //you must use a handler } } ?> |
四 : php下载远程文件类--支持断点续传 (1)
1.功能:支持断点续传的下载,能计算传输率,能控制传输率
2.简易使用方法:
以下为引用的内容: $object = new httpdownload(); |
3.源文件:
以下为引用的内容: <? var $data = null; if ($this->use_auth) //use authentication if (isset($_SERVER['HTTP_RANGE']) || isset($HTTP_SERVER_VARS['HTTP_RANGE'])) if (isset($HTTP_SERVER_VARS['HTTP_RANGE'])) $seek_range = substr($HTTP_SERVER_VARS['HTTP_RANGE'] , strlen('bytes=')); $range = explode('-',$seek_range); if ($range[0] > 0) if ($range[1] > 0) $this->seek_end = intval($range[1]); if (!$this->use_resume) //header("HTTP/1.0 404 Bad Request"); //exit; //return false; } return true; if ($this->data_section && $this->use_resume) function download_ex($size)
try error_log("begin downloadn", 3,"/usr/local/www/apache22/LOGS/apache22_php.err");
//do some clean up $size = $this->data_len; if ($this->data_type == 0) //download from a file $size = filesize($this->data); $res = fopen($this->data,'rb'); $this->header($size,$seek,$this->seek_end); //always use the last seek while (!(connection_aborted() || connection_status() == 1) && $size > 0) }
if ($speed > 0 && ($this->bandwidth > $speed*$packet*1024)) } elseif ($this->data_type == 1) //download from a string $this->sentSize+=$size; echo substr($this->data , 0 , $bufsize); $size -= $bufsize; if ($speed > 0 && ($this->bandwidth > $speed*$packet*1024))
//restore old status
} function set_byfile($dir) { function set_bydata($data) { function set_byurl($data) { function set_lastmodtime($time) { } ?> |
61阅读| 精彩专题| 最新文章| 热门文章| 苏ICP备13036349号-1