提示用户保存为


prompt user to save as

代码如下:

$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的图像。但是,如果用户右键单击并将链接保存为,他们可以将文件保存为他们想要的任何名称。那么,当人们点击链接时,我如何提示"另存为"对话框呢?

可能用户的下载设置设置为"default to X location"。你无法控制这一切。让用户对文件做他们想做的事情,不要试图干涉他/她的工作流程。

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

你不能用你建议的方式做你想做的事。但是,您可以使用以下命令显式地向用户询问文件名:

<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);