提示用户保存为

时间:2011-11-01 08:36:56

标签: php

我的代码如下:

$final_img = ''; //some image manipulations
header('Content-Description: File Transfer');
header('Content-Type: application/octet-image');
header("Content-disposition: attachment; filename=tests.png");

imagepng($final_img);

我的问题是,当人们点击锚点时,它会自动保存名为tests.png的图像。但是如果用户右键单击并保存链接,他们可以将文件保存为他们想要的任何名称。那么,当人们点击链接时如何提示另存为对话框?

3 个答案:

答案 0 :(得分:2)

你做不到。这取决于用户浏览器的设置。

答案 1 :(得分:2)

用户可能已将下载设置设置为“默认为X位置”。你无法控制它。让用户使用文件执行他们想要的操作,并停止尝试插入他/她的工作流程。

答案 2 :(得分:2)

你不能以你的建议方式做你所要求的。但是,您可以使用以下方法明确要求用户输入文件名:

<form action="download.php" method="get">
  <label>File name:</label>
  <input type="text" name="filename" />
  <input type="submit" name="submit" value="Download image" />
</form>

的download.php:

$final_img = ''; //some image manipulations

/* Fetch filename and ensure it ends with '.png'. */
/* @todo strip unacceptable characters, e.g. slashes etc. */
$fileName = trim($_GET['filename']);
if (strtolower(substr($fileName,-4)) != '.png') $fileName .= '.png'

/* Send the headers */
header('Content-Description: File Transfer');
header('Content-Type: image/png');
header("Content-disposition: attachment; filename=$fileName");

/* Send the image data */
imagepng($final_img);