gpt4 book ai didi

php - 如何在PHP中下载现有文件

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:57:11 26 4
gpt4 key购买 nike

我的服务器上有一个 pdf 文件。我想创建这样的链接,用户可以点击它并下载该 pdf 文件。我正在使用 Zend 框架和 Php。

最佳答案

将此代码放入一个 php 文件中并将其命名为 f.e. “下载.php”:

<?php

$fullPath = "path/to/your/file.ext";

if ($fd = fopen ($fullPath, "r")) {

$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);

header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
header("Content-length: $fsize");
header("Cache-control: private");

while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}

fclose ($fd);
exit;

?>

例子:把这种链接放在提供文件下载的文档中:

<a href="download.php?download_file=some_file.pdf">Download here</a>

更多细节:

http://www.finalwebsites.com/forums/topic/php-file-download

关于php - 如何在PHP中下载现有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3572894/

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