2012-04-04 149 views
0

我在php中动态生成图像。该图像具有固定的名称。我想要一个按钮或超链接并点击该按钮,用户应该能够导出图像而不是右键单击并保存为图像选项。问题是,如果是excel,pdf或doc文件,我可以指定文件和浏览器的路径自动请求打开或保存选项,但对于图像,它会在单独的窗口中打开它们。我想要相同的对话框来保存图像像其他文件,如excel,pdf。请帮助我。导出并保存图像对话框

感谢

回答

0

您可以设置标题强制下载(对于PHP):

header("Content-Description: File Transfer"); 
header("Content-Disposition: attachment; filename=$file"); 
header("Content-Type: image/png"); 

然后你就可以读取该文件

readfile($file); 
+0

thanks.it工作 – 2012-04-04 13:49:42

0

你可以做的是力图像下载使用.htaccess如果您使用的是Apache或直接通过php使用标题标签

PHP示例

$file = "PATH TO FILE" ; 
$fileName = basename($file); 
$fileSize = filesize($file); 
set_time_limit(0); 
header("Pragma: public"); 
header("Expires: 0"); // Cache Options 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); // Cache Options 
header("Content-type: application/octet-stream"); 
header("Content-Disposition: filename=\" " . $fileName ."\""); 
header("Content-length: $fileSize"); 
readfile($file);