gpt4 book ai didi

php头下载重定向

转载 作者:行者123 更新时间:2023-12-04 07:05:57 24 4
gpt4 key购买 nike

我正在制作一个有趣的文件共享网站。试图让它在我点击下载时开始下载。而不仅仅是指向 /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>

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

谢谢你

最佳答案

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

<?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头下载重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1123433/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com