dortmundbvb0624 2010-09-28 07:01
浏览 46

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

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&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

See how it parses the & to &amp; ? 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); 
  • 写回答

4条回答 默认 最新

  • doudu9148 2010-09-28 07:06
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错