如何强制下载需要参数的动态图像?

发布于 2024-09-25 08:19:24 字数 1772 浏览 8 评论 0原文

我有一个动态图像,它使用 GD 来添加一些覆盖图像/文本。这将是dynamicImage.php?firstName=Bob&lastName=Sacamano。我希望系统提示我下载该文件,因此我创建了一个 download.php 文件来充当中间人:

//Get the Arguments
$file .= "firstName=".filter_var($_GET['firstName'], FILTER_SANITIZE_STRING);
$file .= "&lastName=".filter_var($_GET['lastName'], FILTER_SANITIZE_STRING);

//get The File Size
$size = intval(sprintf("%u", filesize($file)));

//Header Info to Prompt for Download and name it a .jpg
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-disposition: attachment; filename=dynamicImage.jpg");
header("Content-Length: ".$size);
readfile($file, true);//.$file);

有 2 个问题,首先我收到此错误:

PHP Warning:  filesize() [<a href='function.filesize'>function.filesize</a>]: stat failed for dynamicImage.php?firstName=bob&amp;lastName=Sacamano in /www/download.php on line 19
PHP Warning:  readfile(dynamicImage.php?firstName=bob&amp;lastName=Sacamano) [<a href='function.readfile'>function.readfile</a>]: failed to open stream: No such file or directory in /www/download.php on line 25

查看它如何解析 & 。到&; ?但不仅如此。如果我取出参数并只留下dynamicImage.php,它会提示我下载原始php文件。有没有办法让它运行 PHP 然后下载生成的图像? 顺便说一句,我的dynamicImage.php 结尾为:

header("Content-Type: image/JPEG");
ImageJpeg ($bg);
imagedestroy($bg);

Fixd。我这样改变了我的dynamicImage.php:

if(isset($_GET['download'])){
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-image');
    header("Content-disposition: attachment; filename=dynamicImage.jpg");
}else{
    header("Content-Type: image/JPEG");    
}
ImageJpeg ($bg);
imagedestroy($bg); 

I have a dynamic image which uses GD to throw in some overlay images/text. This would be dynamicImage.php?firstName=Bob&lastName=Sacamano. I want to be prompted to download that file, so I created a download.php file to act as the middle-man:

//Get the Arguments
$file .= "firstName=".filter_var($_GET['firstName'], FILTER_SANITIZE_STRING);
$file .= "&lastName=".filter_var($_GET['lastName'], FILTER_SANITIZE_STRING);

//get The File Size
$size = intval(sprintf("%u", filesize($file)));

//Header Info to Prompt for Download and name it a .jpg
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-disposition: attachment; filename=dynamicImage.jpg");
header("Content-Length: ".$size);
readfile($file, true);//.$file);

There's 2 problems, first I get this error:

PHP Warning:  filesize() [<a href='function.filesize'>function.filesize</a>]: stat failed for dynamicImage.php?firstName=bob&lastName=Sacamano in /www/download.php on line 19
PHP Warning:  readfile(dynamicImage.php?firstName=bob&lastName=Sacamano) [<a href='function.readfile'>function.readfile</a>]: failed to open stream: No such file or directory in /www/download.php on line 25

See how it parses the & to & ? But not only that. If I take out the arguments and just leave dynamicImage.php it prompts me to download the raw php file. Is there a way I can make it Run the PHP and then download the generated image?
BTW My dynamicImage.php ends with:

header("Content-Type: image/JPEG");
ImageJpeg ($bg);
imagedestroy($bg);

Fixd. I altered my dynamicImage.php thusly:

if(isset($_GET['download'])){
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-image');
    header("Content-disposition: attachment; filename=dynamicImage.jpg");
}else{
    header("Content-Type: image/JPEG");    
}
ImageJpeg ($bg);
imagedestroy($bg); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

过度放纵 2024-10-02 08:19:24

我会尝试在最初的答案中添加一些内容到我的评论中。

尝试通过 http 单独调用另一个脚本来下载文件是一种倒退,而且会使一个简单的问题变得过于复杂。

将 DynamicImage.php 的原始代码重构为函数会更容易。然后将该文件作为库包含在 download.php 中,并使用dynamicImage.php 中的函数返回设置了 Content-disposition 标头的图像。

或者,您可以将 download 作为第三个参数添加到dynamicImage.php 脚本中,并在设置该参数时添加 Content-Disposition 标头以从dynamicImage.php 输出。

另请参阅@Novikov 的回答。

I'll try to add some meat to my comment in an initial answer.

Trying to download your file through a separate call to another script over http is backwards and over complicating a simple problem.

It would be easier to refactor your original code for dynamicImage.php into a function. Then include that file as a library in download.php and use the function from dynamicImage.php to return your image with Content-disposition headers set.

Or you could add download as a third argument to your dynamicImage.php script and just add the Content-Disposition header to output form dynamicImage.php when that argument is set.

Also see @Novikov's answer.

世界和平 2024-10-02 08:19:24

您的硬盘上的文件是如何调用的?这就是您应该使用 filesize() 的值。

How is the file called on your hard disk? That's the value you should filesize() on.

梦醒灬来后我 2024-10-02 08:19:24

您正尝试通过 HTTP 从 PHP 脚本内部调用 PHP 脚本吗?非常鲁布·戈德堡。

  1. 您可能需要一个完整路径,例如 http://yoursite/dynamicImage.php,否则 PHP 文件处理函数会将其视为文件系统调用而不是 HTTP 包装器调用。
  2. PHP manual 声明 HTTP 包装器不支持 stat,因此您将无法执行filesize("http://url")

我建议重构dynamicImage.php 中的代码,例如&action=download 将允许您将图像下载为文件而不是jpeg。

You are trying to call a PHP script from within a PHP script through HTTP? Very Rube Goldbergy.

  1. You'll probably want a full path such as http://yoursite/dynamicImage.php, otherwise the PHP file handling functions will treat it as a filesystem call not an HTTP wrapper call.
  2. The PHP manual states that stat isn't supported on HTTP wrappers so you won't be able to do filesize("http://url").

I'd suggest refactoring the code in dynamicImage.php so that for example &action=download will let you download the image as a file rather than a jpeg.

韵柒 2024-10-02 08:19:24

我认为您只有 2 个解决方案:

  1. 第一次使用 dynamicImage.php 生成图片并将其存储到您的服务器中,例如 bob-Sacamano.jpg。但如果你要生成大量图片,它会占用硬盘空间,但这仍然是一个解决方案。
  2. 正如诺维科夫和许多其他人所说,您可能希望在同一脚本中生成并强制下载。 (也许像建议的那样在 get http 调用中使用另一个参数)。

I think you only got 2 solutions :

  1. Generate the picture the first time with your dynamicImage.php and store it into your server as, for example, bob-Sacamano.jpg. But it'll be a hdd space eater if you got a lot of pictures to generate, still, it's a solution.
  2. As Novikov and many other said, you may want to generate and forcing download in the same script. (Maybe with another argument in your get http call like suggested).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文