首页 > 解决方案 > 使用下载属性在 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> 

不管用....

标签: phphtml

解决方案


首先,您必须制作 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 变量中设置您要下载的文件的路径。

我希望您能够下载该文件。


推荐阅读