【发布时间】:2023-03-15 09:55:01
【问题描述】:
我的下载功能有问题。 在我的系统中,我喜欢进行安全下载。 我的根目录下的文件无法通过 url 访问。 我将ftp权限设置为700。只有服务器可以访问。 之后,我制作了一个可以为用户下载文件的功能。 那时我可以对 session 进行安全检查。
但是,当服务器通过此代码下载文件时,该文件会在浏览器中打开。 使用exe,浏览器显示奇怪的字母。
// Bestandsnaam
$file = 'bestanden/test.exe';
// Ophalen bestand
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;
}
当用户访问网址时,我想要一个另存为框。
我想这样使用这个功能:
<a href="www.site.nl/downloadfunction/file">download</a>
有人有解决办法吗?我尝试了 Javascript document.execCommand('SaveAs',null,bestand); 而不是 readfile
但它在 Firefox 中不起作用。
感谢您的帮助!
【问题讨论】:
-
你有这个功能的else部分吗?还要检查文件的路径是否正确,文件是否有被获取的权限。
-
readfile是 PHP,execCommand是 JavaScript。你不能直接交换它们。execCommand('save'也只是 IE,所以这不是一个很好的解决方案。
标签: php javascript codeigniter http-headers download