PHP头下载重定向 [英] php header download redirect

查看:104
本文介绍了PHP头下载重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作文件分享网站以获得乐趣。试图做到这一点,当我点击下载时,它开始下载。而不是只是一个链接到 / files / $ file 即时消息试图做一个头重定向:



download.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)){
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);
出口;
}

?>

文件名:<?= $ row ['name']?>
描述:<?= $ row ['desc']?>

< a href =#>下载此档案< / a>

我在这里卡住了,下一步该怎么办?

谢谢

解决方案

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

 <?php 

$查询= 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('文件不存在');
}

if(intval($ _ GET ['download'])=== 1)
{
//生成服务器头文件
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');
头(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)。'''');
头(Content-Transfer-Encoding:binary);
header('Expires:0');
header('Pragma:no-cache');
header(Content-Length:.filesize($ file));
}
$ data = readfile($ file);
退出($ data);
}
else
{
?>
文件名:<?php echo $ row ['name']; ?>
描述:<?php echo $ row ['desc']; ?>
< a href =<?php echo $ _SERVER ['SCRIPT_NAME'];?>?id =<?php echo $ row ['id'];?>& download = 1 >下载此档案< / a>
<?php
}
?>


I'm making a file sharing site for the fun. Trying to make it so that when I hit download, it starts the download. Instead of just a link to /files/$file im trying to do a header redirect:

download.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>

Im stuck here, what should I do next?

thank you

解决方案

Do you mean something like this, where the page will display the information about the file and then when the user has clicked th link it will download it?

<?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
}
?>

这篇关于PHP头下载重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆