解析PHP实现下载文件的两种方法

yipeiwu_com6年前PHP代码库
方法一:
复制代码 代码如下:

 header('Content-Description: File Transfer');
 header('Content-Type: application/octet-stream');
 header('Content-Disposition: attachment; filename='.basename($filepath));
 header('Content-Transfer-Encoding: binary');
 header('Expires: 0′);
 header('Cache-Control: must-revalidate, post-check=0, pre-check=0′);
 header('Pragma: public');
 header('Content-Length: ' . filesize($filepath));
 readfile($file_path);

方法二:
复制代码 代码如下:

 $fileinfo = pathinfo($filename);
 header('Content-type: application/x-'.$fileinfo['extension']);
 header('Content-Disposition: attachment; filename='.$fileinfo['basename']);
 header('Content-Length: '.filesize($filename));
 readfile($thefile);
 exit();

相关文章

深入php self与$this的详解

先谈parent与self:复制代码 代码如下:<?php/* * Created by YinYiNiao */ class A{ &nb...

php动态函数调用方法

php中可以把函数名通过字符串的方式传递给一个变量,然后通过此变量动态调用函数 下面是一个简单的动态函数调用范例 <html> <head> <titl...

用PHP写的基于Memcache的Queue实现代码

php类代码: 复制代码 代码如下: <?php class MQ{ public static $client; private static $m_real; private...

必须收藏的23个php实用代码片段

在编写代码的时候有个神奇的工具总是好的!下面这里收集了 40+ PHP 代码片段,可以帮助你开发 PHP 项目。 这些 PHP 片段对于 PHP 初学者也非常有帮助,非常容易学习,让我...

php下使用iconv需要注意的问题

string iconv ( string $in_charset , string $out_charset , string $str ) 在使用这个函数进行字符串编码转换时,需要注...