所以我在卖东西,我想在用户访问感谢页面时开始下载文件。
文件的位置存储在$install
,现在当用户访问thanks.php
时,如何自动启动文件下载?
感谢。
答案 0 :(得分:2)
你想要做的大部分事实上都是用Javascript完成的。
您的PHP代码将提供感谢页面,加载后,您需要使用以下标题将页面中隐藏的iFrame指向一个提供文件作为下载的页面:
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=yourfile.txt");
readfile($pathToFile);
答案 1 :(得分:1)
<?php
// place this code inside a php file and call it f.e. "download.php"
$path = $_SERVER['DOCUMENT_ROOT']."/path2file/"; // change the path to fit your websites document structure
$fullPath = $path.$_GET['download_file'];
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf"); // add here more headers for diff. extensions
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
break;
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
// example: place this kind of link into the document where the file download is offered:
// <a href="download.php?download_file=some_file.pdf">Download here</a>
?>
答案 2 :(得分:1)
javascript重定向的替代方法是在HTML中使用meta refresh标记。它甚至在javascript被禁用时也能正常工作。