2013-04-29 129 views
0

我有一个名为generate.php的PHP文件,它根据我设置的一些参数生成PNG电子邮件。可以显示图像,例如使用该图像将返回“myString”文本的图像。通过PHP下载图片

话,我想补充一个链接,允许用户下载图片:

我创建了一个文件的download.php用下面的代码:

header('Content-disposition: attachment; filename=string.png'); 
header('Content-type: image/png'); 
readfile('generatePicture.php?string=' . $_REQUEST["string"]); 

但是,当我下载PNG文件,我似乎无法下载。有什么建议么?

+0

你可以只包含generatePicture.php来调用它吗? – juuga 2013-04-29 19:49:52

+0

我该怎么做? – MrD 2013-04-29 19:53:49

+0

这是使用'readfile()'的问题,因为你的脚本试图直接打开一个文件。您将需要向该位置发出HTTP请求并输出响应。 – 2013-04-29 19:55:06

回答

-2

会话锁定。如果您有两个脚本使用session_start(),则第一个脚本将打开并锁定会话的缓存,第二个脚本必须等待第一个脚本才能打开缓存。

如果您有一个长时间运行的脚本,您不想独占用户的会话,那么请不要在该脚本中调用session_start(),或在完成会话数据后调用session_write_close(),但在您之前开始长时间运行。

要么,要么使用两个不同的浏览器打开两个单独的会话。

+0

...但我从来没有呼吁session_start() – MrD 2013-04-29 19:53:15

-1

我一直在使用这个适中的成功多种文件类型。您需要从文件信息中提取的主要内容是文件类型,文件名和文件大小。

header("Content-Description: PHP Generated Data"); 
    header("Content-type:".$type); 
    header("Content-Disposition: Attachment; Filename=".$name); 
    header("Content-length:".$size); 
    echo $data; 

这可能会给你一些不同的尝试。

+0

这是'echo $ data'部分是麻烦。 OP需要从HTTP源读取数据并输出。 – 2013-04-29 19:55:46

0

当你读取一个PHP文件时,它的源代码就会显示出来。下面我会告诉你我解决这个问题的方法。

假设generatePicture.php样子:

<?php 
$im = imagecreatetruecolor($w, $h); 
// work with the picture 
header('Content-type: image/png'); 
imagepng($im); 
?> 

...它可以与download.php这样集成:

<?php 
$im = imagecreatetruecolor($w, $h); 
// work with the picture 

if(isset($_GET['download'])) 
    header('Content-disposition: attachment; filename=string.png'); 

header('Content-type: image/png'); 
imagepng($im); 
?> 

,而不是链接到download.php?string=loremipsum,链接到generatePicture.php?string=loremipsum&download=1

现在希望它有助于。