PHP download files force download of any file format_PHP tutorial

WBOY
Release: 2016-07-13 10:30:05
Original
1276 people have browsed it

Use PHP to download some files. This is usually done to hide the real download address of the file. Otherwise, it will increase the load on the server. It is better to provide the address of the software directly.

A simple php file downloads the source code. Although it does not support breakpoint resumption, etc., it can meet some common needs. PHP downloading files can actually be achieved with an a tag, such as magento-1.8.1.0.zip. But if you encounter some formats that the browser can recognize, such as .txt, .html, .pdf, etc., then use abc.txt I guess you know what will happen. What's up.

<?<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>?>
Copy after login

Source code for PHP mandatory file download

Provide users with mandatory file download functionality.

<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>
Copy after login

You will definitely laugh at me. Is it worth saying that "downloading files" is so simple? Of course it's not as simple as imagined. For example, if you want customers to fill out a form before they can download a certain file, your first idea must be to use the "Redirect" method. First check whether the form has been filled in and complete, and then point the URL to the file. , so that customers can download it, but if you want to make an e-commerce website about "online shopping" and consider security issues, you don't want users to directly copy the URL to download the file. The author recommends that you use PHP to directly read the actual file and then download it. method to do it. The procedure is as follows:

<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>
Copy after login

If the file path is an "http" or "ftp" URL, the source code will change slightly. The procedure is as follows:

<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>
Copy after login

In this way, you can use PHP to directly output the file.

However, be sure to note: Header information is equivalent to first browsing the file information at high speed, and then downloading the information on the browser to the attachment. Therefore, if you are in an MVC-mode application, the view page must not have any content. Otherwise, the relevant content of the view page will be downloaded together with the content of the file, resulting in the downloaded file being unusable.
Here is my program:

<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>
Copy after login

At this time, the download.phtml page must be completely blank. Never include any content (including canned messages like:

<!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>)否则,这些信息都将被下载到下载文件中,导致文件不能使用。
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/767673.htmlTechArticleUse php to download some files. This is usually done to hide the real download address of the file, otherwise it will increase the number of servers. burden, it is better to provide the address of the software directly. A simple...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template