【问题标题】:php header download redirectphp头文件下载重定向
【发布时间】:2009-07-14 04:34:17
【问题描述】:

我正在制作一个文件共享网站来取乐。试图让它在我点击下载时开始下载。而不仅仅是指向/files/$file 的链接,我正在尝试进行标头重定向:

下载.php

/**
* File Download
*/

$query = mysql_query("SELECT id,name,desc FROM files WHERE id = ".intval($_GET['id']));
$row = mysql_fetch_assoc($query);

$file = $row['name'];

if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
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($file));
ob_clean();
flush();
readfile($file);
exit;
}

?>

Filename: <?=$row['name']?>
Desc: <?=$row['desc']?>

<a href="#">Download this file</a>

我卡在这里了,接下来我该怎么办?

谢谢

【问题讨论】:

  • 请注意,“readfile 和 fpassthru 比使用 feof/echo fread 循环慢约 55%”。根据php.net上的某人
  • @Alex L:哇,真的吗?我认为 readfile 更快,因为它可以映射...你自己测试过吗?

标签: php header


【解决方案1】:

您的意思是这样的吗,页面将在哪里显示有关文件的信息,然后当用户单击链接时它会下载它?

<?php

$query = mysql_query("SELECT id,name,desc FROM files WHERE id = ".intval($_GET['id']));
$row = mysql_fetch_assoc($query);

$file = $row['name'];

if(!file_exists($file))
{
    exit('File does not exist');
}

if(intval($_GET['download'])===1)
{
    // Generate the server headers
    if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
    {
        header('Content-Type: "application/octet-stream"');
        header('Content-Disposition: attachment; filename="'.basename($file).'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header("Content-Transfer-Encoding: binary");
        header('Pragma: public');
        header("Content-Length: ".filesize($file));
    }
    else
    {
        header('Content-Type: "application/octet-stream"');
        header('Content-Disposition: attachment; filename="'.basename($file).'"');
        header("Content-Transfer-Encoding: binary");
        header('Expires: 0');
        header('Pragma: no-cache');
        header("Content-Length: ".filesize($file));
    }
    $data = readfile($file);
    exit($data);
}
else
{
?>
    Filename: <?php echo $row['name']; ?>
    Desc: <?php echo $row['desc']; ?>
    <a href="<?php echo $_SERVER['SCRIPT_NAME']; ?>?id=<?php echo $row['id']; ?>&download=1">Download this file</a>
    <?php
}
?>

【讨论】:

    【解决方案2】:

    我没有按照您的要求进行操作。标头重定向如下所示:

    header("Location: /path/to/new/page");
    

    如果你想下载现有信息,看起来你需要在你的 url 中用一个 id 来点击它:

    <a href="file.php?id=<?=$file_id ?>">  
    

    $file_id 的位置是您要查找的文件的 ID。就个人而言,我会将其放在显示列表和下载链接的页面中的另一个文件中,但 YMMV。

    【讨论】:

    【解决方案3】:

    您只能在任何 html 被推送之前进行重定向。

    基本上在

    Filename: <?=$row['name']?>
    Desc: <?=$row['desc']?>
    
    <a href="#">Download this file</a>
    

    被发送到浏览器,你用php执行重定向的机会就结束了。也许 Javascript 重定向更合适? Tutorial on Javascript redirects.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-23
      • 2012-11-10
      相关资源
      最近更新 更多