在php下载图像文件

时间:2017-07-28 16:09:01

标签: php

我有一个代码,它有效,我用download.php保存了它,但我不知道,如何下载名为simpletext.jpg的图像文件存在于上述文件的同一个根目录下,我非常感谢你,如果你写了实际的路径,怎么做,代码在下面....

$file_name = "a.txt";

// extracting the extension:
$ext = substr($file_name, strpos($file_name,'.')+1);

header('Content-disposition: attachment; filename='.$file_name);

if(strtolower($ext) == "txt")
{
    header('Content-type: text/plain'); // works for txt only
}
else
{
    header('Content-type: application/'.$ext); // works for all extensions 
except txt
}
readfile($decrypted_file_path);

3 个答案:

答案 0 :(得分:1)

<a href="localhost:8888/files/download.jpg" download>Click here to download the file</a>

用你的文件路径替换href ..

希望这会有所帮助

答案 1 :(得分:0)

<a href="/path/file_name.jpg" download="download_file_name">download</a>

download_file_name将替换file_name

答案 2 :(得分:0)

您可以使用htaccess文件强制下载图像文件。看到这个类似的问题:

使用Htaccess强制下载[Forcing a download using <filesMatch> in htaccess at WWW root