如何强制下载需要参数的动态图像?
我有一个动态图像,它使用 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&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
查看它如何解析 & 。到&; ?但不仅如此。如果我取出参数并只留下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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会尝试在最初的答案中添加一些内容到我的评论中。
尝试通过 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.
您的硬盘上的文件是如何调用的?这就是您应该使用 filesize() 的值。
How is the file called on your hard disk? That's the value you should filesize() on.
您正尝试通过 HTTP 从 PHP 脚本内部调用 PHP 脚本吗?非常鲁布·戈德堡。
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.
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.
我认为您只有 2 个解决方案:
I think you only got 2 solutions :