[英]PHP-Download doesn't work properly
I want to dowload as a web-client a photo from my webserver with PHP. 我想通过PHP从我的网络服务器下载一个网络客户端照片。
Therefore, I've written the following PHP-Script: 因此,我编写了以下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 is the name of the file, like screenshot_03.06.2016-19:36:50.jpg ) ( $ filename是文件的名称,如screenshot_03.06.2016-19:36:50.jpg )
The problem is, the content of this downloaded image is not the image itself, it's the code from the current website. 问题是,这个下载图像的内容不是图像本身,而是来自当前网站的代码。 The download works fine, but I cannot open the image properly ...
下载工作正常,但我无法正常打开图像...
OK. 好。
After some time, I found a working solution. 过了一段时间,我找到了一个有效的解决方案
My problem was, that I wrote the header-commands AFTER some output. 我的问题是,我在一些输出之后编写了header-commands。 According to the php documentation, this is forbidden.
根据php文档,这是禁止的。
So all I did is this, I created a new .php-file and checked if the session (in which I previously stored the needed information - filename and download-command) was set correct. 所以我所做的就是这个,我创建了一个新的.php文件,并检查会话(我之前存储了所需信息 - filename和download-command)是否设置正确。 On the previous page, I received the POST-information, stored it into the session and redirected (also with header-command) to the newly created php-file.
在上一页中,我收到了POST信息,将其存储到会话中并重定向(也使用header-command)到新创建的php文件。
Then I wrote these header-commands: 然后我写了这些header-commands:
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
readfile($filename);
And now, the download works from all browsers (also IE!). 现在,下载适用于所有浏览器(也是IE!)。
Here is all my code, I hope this could help someone: 这是我的所有代码,我希望这可以帮助某人:
1st page (where I receive the POST-infos): 第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>...
2nd, new created php-page (where I actually download the image): 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"]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.