javascript - 当我在 php 中单击图像名或文件名时,我想下载文件
问题描述
$path = "https://surv-translation.com/assets/uploads/files/";
$filename = "1544615073screenshot-trustwortha.com-2018.11.29-16-37-46.png";
header('Content-Transfer-Encoding: binary'); // For Gecko browsers mainly
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($path)) . ' GMT');
header('Accept-Ranges: bytes'); // For download resume
header('Content-Length: ' . filesize($path)); // File size
header('Content-Encoding: none');
header('Content-Type: application/pdf'); // Change this mime type if the file is not PDF
header('Content-Disposition: attachment; filename=' . $filename); // Make the browser display the Save As dialog
readfile($path);
exit;
我尝试使用此代码,但没有成功,我想下载任何文件,如 docx、pdf、ppt 或图像等。请问您能帮帮我吗?
解决方案
您可以尝试使用此代码下载任何文件,如doc、pdf、ppt或图像等
if (file_exists($filepath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($filepath) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
flush(); // Flush system output buffer
readfile($filepath);
exit;
}
推荐阅读
- powerbi - Power BI 本地报表服务器 - 如何增加报表会话超时
- laravel - 使用 Laravel Sail 在 ubuntu 21.04 上定位 msodbcsql17 的问题
- python - 如何添加具有非均匀间隔和不同比例的辅助 matplotlib 轴
- c++ - 在共享库中隐藏私有符号的定义
- java - JDK14中的AbstractQueuedSynchronizer在使用setPrevRelaxed时如何保证内存可见性?
- c# - 有没有办法忽略反序列化中的某些字符?
- java - Springfox 3 OpenAPI 3 不发送带有请求的授权标头
- javascript - React Callservice 脚本不会从进行 API 调用的 Node.js 脚本中检索所需的信息
- angular - Angular 11 > 12 TypeError:无法读取未定义的属性“匹配”
- azure-blob-storage - Azure Blob:无法通过 java SDK 生成下载链接