强制从不同的服务器下载图像html



我有一个链接,点击链接后,用户可以下载图像。现在,点击链接后,用户只能查看图像。

我如何让浏览器在用户点击链接后制作,它会强制下载图像

这是代码

<html>
<head>
<title> Download-Images</title>
</head>
<body>
<p> Click the link ! You can download Image </p>
<a download="logo.png" href="https://w3schools.com/images/myw3schoolsimage.jpg" title="Logo title" target="_blank">
link
</a>
</body>
</html>

我已经尝试在我的html代码下面从这里和这里添加php代码,但它不起作用。

你可以试试这个。我已经在本地主机上进行了测试,它成功了。

html:

<html>
<head>
<title> Download-Images</title>
</head>
<body>
<p> Click the link ! You can download Image </p>
<a href="download.php?url=https://w3schools.com/images/myw3schoolsimage.jpg" title="Logo title">
link
</a>
</body>
</html>

下载.php

<?php
$url = $_GET['url'];
if ($url != '')
{
$filename = end(explode('/', $url));
$fileext = strtolower(end(explode('.', $filename)));

if ($fileext == 'jpg' || $fileext == 'jpeg') { $type = 'image/jpeg'; }
if ($fileext == 'png') { $type = 'image/png'; }
if ($fileext == 'gif') { $type = 'image/gif'; }

//$size = filesize($url);
header("Content-Type: $type");
header("Content-disposition: attachment; filename="" . $filename . """);
header("Content-Transfer-Encoding: binary");
//header("Content-Length: " . $size);
readfile($url);
}else{
echo 'url is blank';
}
?>

最新更新