php - 使用下载属性在 href 中下载图像,但在其他服务器上无法使用图像 url
问题描述
我想在点击href标签时下载图片
我在标签中使用下载属性,但不适用于其他服务器图像。
例子 -
正在工作中
<a href="myfolder/googlelogo_color_272x92dp.png" target="_blank" download>Download</a>
但
<a href="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" target="_blank" download>Download</a>
不管用....
解决方案
首先,您必须制作 2 个新文件。
1个文件是:link.php
在这个文件中写代码
<?php
echo '<a href="DownloadImage.php">Download</a>';
?>
2个文件是:DownloadImage.php
在此 (DownloadImage.php) 文件中编写代码
<?php
$filePath = "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png";
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=" . basename($filePath));
readfile ($filePath);
exit();
?>
在这里,在 $filePath 变量中设置您要下载的文件的路径。
我希望您能够下载该文件。
推荐阅读
- java - Java 11 中 String trim() 和 strip() 方法的区别
- python - 提高 elasticsearch-py 扫描的速度
- zap - 导出/导入 OWASP ZAP 被动扫描规则
- jmeter - Pypi 性能测试。Jmeter 代理返回错误# 501
- javascript - 如果跨度标记退出jQuery中的某个值,如何隐藏父元素内部文本
- php - mysqli 事务查询优于普通 php mysqli 查询
- json - 如何在 swift 4 中从多个字典中写入多个 JSON 对象
- c# - ApplicationMessageProcessed 事件未在托管 MQTTnet 客户端中触发
- django - 姜戈;如何在模板中调试 {% include '' %} 标签
- c# - 在 Xamarin 表单中创建 Firestore 客户端崩溃 [无法使用令牌 0100003b 解析类型]