我希望使用PHP从网络服务器下载网络客户端照片。
因此,我编写了以下PHP脚本:
header('Content-Description: File Transfer');
header('Content-Type: image/jpg');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
readfile("/var/www/_old/pictures/".$filename);
( $ filename 是文件的名称,例如 screenshot_03.06.2016-19:36:50.jpg )
问题是,此下载图像的内容不是图像本身,而是来自当前网站的代码。下载工作正常,但我无法正常打开图像...
答案 0 :(得分:0)
行。
过了一段时间,我找到了一个有效的解决方案。
我的问题是,我在一些输出后编写了header-commands。根据php文档,这是禁止的。
所以我所做的就是这个,我创建了一个新的.php文件并检查会话(我之前存储了所需信息 - filename和download-command)是否设置正确。在上一页中,我收到了POST信息,将其存储到会话中并重定向(也使用header-command)到新创建的php文件。
然后我写了这些header-commands:
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
readfile($filename);
现在,下载适用于所有浏览器(也是IE!)。
这是我的所有代码,我希望这可以帮助某人:
第1页(我收到POST信息):
session_start();
...
...
if(isset($_POST["galerie_submitBt"])){
if($_POST["galerie_submitBt"] === "Herunterladen") { //Das Herunterladen muss in einer eigenen Datei passieren, bevor jeglichen Outputs
$_SESSION["download_Screenshot"] = true;
$_SESSION["filename"] = "/var/www/_old/pictures/".$_POST["filename"];
header("Location:modules/php/download_Screenshot.php");
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>...
2,新创建的php页面(我实际下载图像的地方):
session_start();
if(isset($_SESSION["download_Screenshot"])){
if(isset($_SESSION["filename"]) && file_exists($_SESSION["filename"])) {
$filename = $_SESSION["filename"]; //Filename also includes the path! (absolute path)
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
readfile($filename);
unset($_SESSION["filename"]);
unset($_SESSION["download_Screenshot"]);
}
}