用php下载一些文件,一般就是为了隐藏文件的真实下载地址才需要这样,否则这样会增加服务器负担,不如直接提供软件的地址。
一个简单的php文件下载源代码,虽不支持断点续传等,但是可以满足一些常用的需求了。php下载文件其实用一个a标签就能实现,比如 magento-1.8.1.0.zip 。但是遇到一些浏览器能识别的格式,比如.txt,.html,.pdf等,再用abc.txt 想必也知道会发生什么了。
<?<span>php </span><span>/*</span><span>* * 文件下载 * *</span><span>*/</span> <span>header</span>("Content-type:text/html;charset=utf-8"<span>); download(</span>'web/magento-1.8.1.0.zip', 'magento下载'<span>); </span><span>function</span> download(<span>$file</span>, <span>$down_name</span><span>){ </span><span>$suffix</span> = <span>substr</span>(<span>$file</span>,<span>strrpos</span>(<span>$file</span>,'.')); <span>//</span><span>获取文件后缀</span> <span>$down_name</span> = <span>$down_name</span>.<span>$suffix</span>; <span>//</span><span>新文件名,就是下载后的名字 //判断给定的文件存在与否 </span> <span>if</span>(!<span>file_exists</span>(<span>$file</span><span>)){ </span><span>die</span>("您要下载的文件已不存在,可能是被删除"<span>); } </span><span>$fp</span> = <span>fopen</span>(<span>$file</span>,"r"<span>); </span><span>$file_size</span> = <span>filesize</span>(<span>$file</span><span>); </span><span>//</span><span>下载文件需要用到的头</span> <span>header</span>("Content-type: application/octet-stream"<span>); </span><span>header</span>("Accept-Ranges: bytes"<span>); </span><span>header</span>("Accept-Length:".<span>$file_size</span><span>); </span><span>header</span>("Content-Disposition: attachment; filename=".<span>$down_name</span><span>); </span><span>$buffer</span> = 1024<span>; </span><span>$file_count</span> = 0<span>; </span><span>//</span><span>向浏览器返回数据 </span> <span>while</span>(!<span>feof</span>(<span>$fp</span>) && <span>$file_count</span> < <span>$file_size</span><span>){ </span><span>$file_con</span> = <span>fread</span>(<span>$fp</span>,<span>$buffer</span><span>); </span><span>$file_count</span> += <span>$buffer</span><span>; </span><span>echo</span> <span>$file_con</span><span>; } www.jbxue.com </span><span>fclose</span>(<span>$fp</span><span>); } </span>?>
PHP强制性文件下载的源代码
为用户提供强制性的文件下载功能。
<span>/*</span><span>******************* *@file - path to file </span><span>*/</span> <span>function</span> force_download(<span>$file</span><span>) { </span><span>if</span> ((<span>isset</span>(<span>$file</span>))&&(<span>file_exists</span>(<span>$file</span><span>))) { </span><span>header</span>("Content-length: ".<span>filesize</span>(<span>$file</span><span>)); </span><span>header</span>('Content-Type: application/octet-stream'<span>); </span><span>header</span>('Content-Disposition: attachment; filename="' . <span>$file</span> . '"'<span>); </span><span>readfile</span>("<span>$file</span>"<span>); } </span><span>else</span><span> { </span><span>echo</span> "No file selected"<span>; } }</span>
你一定会笑我"下载文件"如此简单都值得说?当然并不是想象那么简单。例如你希望客户要填完一份表格,才可以下载某一文件,你第一个想法一定是用 "Redirect"的方法,先检查表格是否已经填写完毕和完整,然后就将网址指到该文件,这样客户才能下载,但如果你想做一个关于"网上购物"的电子商务网站,考虑安全问题,你不想用户直接复制网址下载该文件,笔者建议你使用PHP直接读取该实际文件然后下载的方法去做。程序如下:
<span>$file_name</span> = "info_check.exe"<span>; </span><span>$file_dir</span> = "/public/www/download/"<span>; </span><span>if</span> (!<span>file_exists</span>(<span>$file_dir</span> . <span>$file_name</span>)) { <span>//</span><span>检查文件是否存在</span> <span>echo</span> "文件找不到"<span>; </span><span>exit</span><span>; } </span><span>else</span><span> { </span><span>$file</span> = <span>fopen</span>(<span>$file_dir</span> . <span>$file_name</span>,"r"); <span>//</span><span> 打开文件 // 输入文件标签 www.jbxue.com</span> <span>Header</span>("Content-type: application/octet-stream"<span>); </span><span>Header</span>("Accept-Ranges: bytes"<span>); </span><span>Header</span>("Accept-Length: ".<span>filesize</span>(<span>$file_dir</span> . <span>$file_name</span><span>)); </span><span>Header</span>("Content-Disposition: attachment; filename=" . <span>$file_name</span><span>); </span><span>//</span><span> 输出文件内容</span> <span>echo</span> <span>fread</span>(<span>$file</span>,<span>filesize</span>(<span>$file_dir</span> . <span>$file_name</span><span>)); </span><span>fclose</span>(<span>$file</span><span>); </span><span>exit</span><span>; } </span>
而如果文件路径是"http" 或者 "ftp" 网址的话,则源代码会有少许改变,程序如下:
<span>$file_name</span> = "info_check.exe"<span>; </span><span>$file_dir</span> = "http://www.jbxue.com/"<span>; </span><span>$file</span> = @ <span>fopen</span>(<span>$file_dir</span> . <span>$file_name</span>,"r"<span>); </span><span>if</span> (!<span>$file</span><span>) { </span><span>echo</span> "文件找不到"<span>; } </span><span>else</span><span> { </span><span>Header</span>("Content-type: application/octet-stream"<span>); </span><span>Header</span>("Content-Disposition: attachment; filename=" . <span>$file_name</span><span>); </span><span>while</span> (!<span>feof</span> (<span>$file</span><span>)) { </span><span>echo</span> <span>fread</span>(<span>$file</span>,50000<span>); } </span><span>fclose</span> (<span>$file</span><span>); } </span>
这样就可以用PHP直接输出文件了。
但,一定要注意:Header信息相当于先将文件信息高速浏览器,然后,再把浏览器上的信息下载到附件中。所以,如果在MVC模式的应用程序中,view页一定不要有任何内容,否则,view页的相关内容会随着文件的内容一同被下载,导致下载后的文件不能使用。
下面是我的程序:
<span>public</span> <span>function</span><span> downloadAction() { </span><span>if</span> (<span>isset</span>(<span>$_GET</span>['mriID'<span>])) { </span><span>$this</span>->view->mriID=(<span>get_magic_quotes_gpc</span>())?<span>$_GET</span>['mriID']:<span>addslashes</span>(<span>$_GET</span>['mriID'<span>]); } </span><span>if</span> (<span>isset</span>(<span>$_GET</span>['dicomID'<span>])) { </span><span>$this</span>->view->dicomID=(<span>get_magic_quotes_gpc</span>())?<span>$_GET</span>['dicomID']:<span>addslashes</span>(<span>$_GET</span>['dicomID'<span>]); } </span><span>if</span> (<span>isset</span>(<span>$_GET</span>['JPGID'<span>])) { </span><span>$this</span>->view->JPGID=(<span>get_magic_quotes_gpc</span>())?<span>$_GET</span>['JPGID']:<span>addslashes</span>(<span>$_GET</span>['JPGID'<span>]); } www.jbxue.com </span><span>$dicomfile</span>=<span>new</span><span> dicomfile(); </span><span>$jpgfile</span>=<span>new</span><span> jpgfile(); </span><span>$mri</span>=<span>new</span><span> mri(); </span><span>if</span>(<span>$this</span>->view-><span>dicomID) { </span><span>$filename</span>=<span>$dicomfile</span>->find(<span>$this</span>->view->dicomID)-><span>toArray(); </span><span>$filename</span>=<span>$filename</span>[0]['filename'<span>]; } </span><span>else</span> <span>if</span>(<span>$this</span>->view-><span>JPGID) { </span><span>$filename</span>=<span>$jpgfile</span>->find(<span>$this</span>->view->JPGID)-><span>toArray(); </span><span>$filename</span>=<span>$filename</span>[0]['JPGname'<span>]; } </span><span>$dir</span>=<span>$mri</span>->find(<span>$this</span>->view->mriID)-><span>toArray(); </span><span>$dir</span>=<span>$dir</span>[0]['dicom_path'<span>]; </span><span>$file</span>=<span>$dir</span>.'/'.<span>$filename</span><span>; </span><span>if</span> (!<span>file_exists</span>(<span>$file</span><span>)) { </span><span>echo</span> "the file does not exist!"<span>; </span><span>exit</span><span>(); } </span><span>$file_size</span>=<span>filesize</span>(<span>$file</span><span>); </span><span>header</span>("Content-type: application/octet-stream"<span>); </span><span>header</span>("Accept-Ranges: bytes"<span>); </span><span>header</span>("Accept-Length:". <span>$file_size</span><span>); </span><span>header</span>("Content-Disposition: attachment; filename=".<span>$filename</span><span>); </span><span>$fp</span>=<span>fopen</span>(<span>$file</span>,"r"<span>); </span><span>if</span> (!<span>$fp</span><span>) </span><span>echo</span> "can't open file!"<span>; </span><span>$buffer_size</span>=1024<span>; </span><span>$cur_pos</span>=0<span>; </span><span>while</span> (!<span>feof</span>(<span>$fp</span>)&&<span>$file_size</span>-<span>$cur_pos</span>><span>$buffer_size</span><span>) { </span><span>$buffer</span>=<span>fread</span>(<span>$fp</span>,<span>$buffer_size</span><span>); </span><span>echo</span> <span>$buffer</span><span>; </span><span>$cur_pos</span>+=<span>$buffer_size</span><span>; } </span><span>$buffer</span>=<span>fread</span>(<span>$fp</span>,<span>$file_size</span>-<span>$cur_pos</span><span>); </span><span>echo</span> <span>$buffer</span><span>; </span><span>fclose</span>(<span>$fp</span><span>); }</span>
此时,download.phtml页面一定要是完全空白的。千万不要有任何内容(包括如下的固定信息:
<!DOCTYPE html <span>PUBLIC</span> "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title>)否则,这些信息都将被下载到下载文件中,导致文件不能使用。