我用它来下载文件:
<a href="download_init.php?Down=01.zip">download here</a>
download_init.php:
<?php
$Down=$_GET['Down'];
?>
<html>
<head>
<meta http-equiv="refresh" content="0;url=<?php echo $Down; ?>">
</head>
<body>
</body>
</html>
有没有办法在点击没有ajax的链接时阻止浏览器网址更改?
download_init.php?Down=01.zip
点击此处:http://www.firegrid.co.uk/scripts/download/index.php
点击第一个链接,网址不会改变,与其他链接不同。
答案 0 :(得分:1)
为锚标记添加download
属性:
<a href="download_init.php?Down=01.zip" download>download here</a>
的详细信息
如果您想使用header
,请参阅this link
答案 1 :(得分:1)
您可以使用少量header
功能执行实际下载,如下面的代码所示。
然而;首先,您可能需要在应用程序的根目录下创建一个任意处理文件(例如:download_init.php
)。现在在download_init.php
文件中,您可以添加如下内容:
<?php
// CHECK THAT THE `d` PARAMETER IS SET IN THE GET SUPER-GLOBAL:
// THIS PARAMETER HOLDS THE PATH TO THE DOWNLOAD-FILE...
// IF IT IS SET, PROCESS THE DOWNLOAD AND EXIT...
if(isset($_GET['d']) && $_GET['d']){
processDownload($_GET['d']);
exit;
}
function processDownload($pathToDownloadFile, $newName=null) {
$type = pathinfo($pathToDownloadFile,
PATHINFO_EXTENSION);
if($pathToDownloadFile){
if(file_exists($pathToDownloadFile)){
$size = @filesize($pathToDownloadFile);
$newName = ($newName) ? $newName . ".{$type}" :basename($pathToDownloadFile);
header('Content-Description: File Transfer');
header('Content-Type: ' . mime_content_type ($pathToDownloadFile ));
header('Content-Disposition: attachment; filename=' . $newName);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size);
return(readfile($pathToDownloadFile));
}
}
return FALSE;
}
然而,这意味着您的链接现在会有如此不同的href
值:
<!-- THIS WOULD TRIGGER THE DOWNLOAD ONCE CLICKED -->
<a href="download_init.php?d=path_to_01.zip">download here</a>
如果你发现这种Header方法对你的目的来说太无关紧要了; @Sanchit Gupta提供了HTML5 download
属性....