views:

33

answers:

4

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

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

Flavius
A: 

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.

Novikov
Brilliant! I edited my post to reflect the changes I made. It worked perfectly. And thank you for making me google Rube Goldberg
+1  A: 

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.

thomasmalt
A: 

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).
Chouchenos