Php 有效下载的空文件-有什么问题?

Php 有效下载的空文件-有什么问题?,php,download,Php,Download,我使用以下代码强制下载php中的文件 <?PHP $id =$_GET['id']; $query = mysql_query("SELECT * FROM `blog` WHERE id = '" . $id . "' LIMIT 1")or die(mysql_error()); while($row = mysql_fetch_assoc($query)){ $file_name = $row[url]; // "wwyypyv6.pdf"

我使用以下代码强制下载php中的文件

<?PHP
    $id =$_GET['id'];

$query = mysql_query("SELECT * FROM `blog` WHERE  id = '" . $id . "' LIMIT 1")or die(mysql_error());
while($row = mysql_fetch_assoc($query)){

        $file_name = $row[url]; //  "wwyypyv6.pdf"
        $file_type = $row[type]; // "application/pdf"
        $file_url = 'http://emfhal.hostech.co.il/upload/' . $file_name;
    }
        header("Content-Type: " . $file_type);
        header("Content-Disposition: attachment; filename=\"$file_name");
        readfile($file_url);
        exit();


    ?>
正确的解决方案:

// grab the requested file's name
$file_name = $_GET['file'];

// make sure it's a file before doing anything!
if(is_file($file_name)) {

    /*
        Do any processing you'd like here:
        1.  Increment a counter
        2.  Do something with the DB
        3.  Check user permissions
        4.  Anything you want!
    */

    // required for IE
    if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }

    // get the file mime type using the file extension
    switch(strtolower(substr(strrchr($file_name, '.'), 1))) {
        case 'pdf': $mime = 'application/pdf'; break;
        case 'zip': $mime = 'application/zip'; break;
        case 'jpeg':
        case 'jpg': $mime = 'image/jpg'; break;
        default: $mime = 'application/force-download';
    }
    header('Pragma: public');   // required
    header('Expires: 0');       // no cache
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file_name)).' GMT');
    header('Cache-Control: private',false);
    header('Content-Type: '.$mime);
    header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '.filesize($file_name));    // provide file size
    header('Connection: close');
    readfile($file_name);       // push it out
    exit();
}正确的解决方案:

// grab the requested file's name
$file_name = $_GET['file'];

// make sure it's a file before doing anything!
if(is_file($file_name)) {

    /*
        Do any processing you'd like here:
        1.  Increment a counter
        2.  Do something with the DB
        3.  Check user permissions
        4.  Anything you want!
    */

    // required for IE
    if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }

    // get the file mime type using the file extension
    switch(strtolower(substr(strrchr($file_name, '.'), 1))) {
        case 'pdf': $mime = 'application/pdf'; break;
        case 'zip': $mime = 'application/zip'; break;
        case 'jpeg':
        case 'jpg': $mime = 'image/jpg'; break;
        default: $mime = 'application/force-download';
    }
    header('Pragma: public');   // required
    header('Expires: 0');       // no cache
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file_name)).' GMT');
    header('Cache-Control: private',false);
    header('Content-Type: '.$mime);
    header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '.filesize($file_name));    // provide file size
    header('Connection: close');
    readfile($file_name);       // push it out
    exit();

}

为正在读取的本地文件使用相对或绝对路径,或者如果使用http,则为其启用fopen包装:

此选项启用支持访问的URL感知fopen包装器 类似URL对象的文件。默认包装器用于访问 使用ftp或http协议的远程文件,一些扩展名如zlib 可以注册其他包装

在php.ini文件中添加/修改此行:

allow_url_fopen = On

为正在读取的本地文件使用相对或绝对路径,或者如果使用http,则为其启用fopen包装器:

此选项启用支持访问的URL感知fopen包装器 类似URL对象的文件。默认包装器用于访问 使用ftp或http协议的远程文件,一些扩展名如zlib 可以注册其他包装

在php.ini文件中添加/修改此行:

allow_url_fopen = On


@aligarian你看不到代码吗?你缺少许多必需的标题。另外,如果在大多数情况下得到一个空文件,则文件路径似乎不正确。@Rikesh我现在编辑。。。我想问题可能是在目录“upload/”中检查您在
$file\u url
中获得的路径并在url中运行该路径。@Rikesh我检查了url几次…@aligarian您没有看到代码吗?您缺少许多必需的标题。另外,如果在大多数情况下得到一个空文件,则文件路径似乎不正确。@Rikesh我现在编辑。。。我想问题可能是它在一个dir“upload/”中,检查你在
$file\u url
中得到的路径并在url中运行它。@Rikesh我检查了url几次…它不工作。。。我试着让你在google中找到它,同样的问题是…检查文件是否存在,你的php文件是否编码为“UTF-8,不带BOM”。请回显你的路径,并检查浏览器url+路径,看看文件是否可下载。@FalkS。。你知道另一种强制下载的方式吗?这一行结束了5个小时的拉扯默认值:$mime='application/force download';'它不起作用。。。我试着让你在google中找到它,同样的问题是…检查文件是否存在,你的php文件是否编码为“UTF-8,不带BOM”。请回显你的路径,并检查浏览器url+路径,看看文件是否可下载。@FalkS。。你知道另一种强制下载的方式吗?这一行结束了5个小时的拉扯默认值:$mime='application/force download';'ini设置(“自动检测线结束”,真);你说要把它粘贴到我的代码?ini_集合(“自动检测线_结束”,true);你说把这个粘贴到我的代码里?