强制下载外部或内部文件

时间:2019-11-26 22:01:01

标签: php download

强制下载jpg之类的外部或内部文件 外部或内部文件的示例方法1

https://example.com/photos/photo.jpg/download?force=true
https://pixabay.com/images/download/wintry-69661.jpg?attachment

另外,此方法将生成下载链接

https://www.pexels.com/photo/2755165/download/

我知道此方法2 :但是我需要上述方法1。

https://download.example.com/images/download.php?file=9284.jpg

我有多个代码可以像方法2一样强制下载。但是我需要方法1。

我正在使用此PHP强制下载方法2

<?php
if(!empty($_GET['file'])){
$fileName = basename($_GET['file']);
$filePath = '/home/download.example.com/images/'.$fileName;
if(!empty($fileName) && file_exists($filePath)){
// Define headers
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$fileName");
header("Content-Type: image/jpeg");
header("Content-Transfer-Encoding: binary");
// Read the file
readfile($filePath);
exit;
}else{
echo 'The file does not exist.';
}
}
?>

谢谢

0 个答案:

没有答案