我有图像列表,我想要一个"下载"链接以及每个图像,以便用户可以下载图像.
有人可以指导我如何为PHP中的任何文件提供下载链接?
编辑
我想在点击下载链接时显示下载面板我不想导航到要在浏览器上显示的图像
Joh*_*ker 30
如果要强制下载,可以使用以下内容:
<?php
// Fetch the file info.
$filePath = '/path/to/file/on/disk.jpg';
if(file_exists($filePath)) {
$fileName = basename($filePath);
$fileSize = filesize($filePath);
// Output headers.
header("Cache-Control: private");
header("Content-Type: application/stream");
header("Content-Length: ".$fileSize);
header("Content-Disposition: attachment; filename=".$fileName);
// Output file.
readfile ($filePath);
exit();
}
else {
die('The provided file path is not valid.');
}
?>
Run Code Online (Sandbox Code Playgroud)
如果您只是使用普通链接链接到此脚本,则将下载该文件.
顺便提一下,上面的代码片段需要在页面的开头执行(在任何标题或HTML输出发生之前).如果您决定创建一个基于此的函数来下载任意文件,请注意 - 您需要确保您阻止目录遍历(realpath很方便),只允许从定义区域内下载等,如果您接受来自$ _GET或$ _POST的输入.
| 归档时间: |
|
| 查看次数: |
25062 次 |
| 最近记录: |