Mirkules Posted October 18, 2007 Share Posted October 18, 2007 Hello all, Quick question: say that I have a PHP file that generates an image using the GD libraries with a header content type set to image/png (or jpeg or whatever). I have no problems displaying the image, but when a user attempts to download the image it has the extension .php. I would like the user to be able to save the file with a proper file extension (like image.jpeg). Is there any way to do this? Ultimately, I also want to do this for text/plain and application/excel (this is the most important one). Thanks in advance, Mirkules Quote Link to comment https://forums.phpfreaks.com/topic/73834-solved-dynamically-changing-script-extension/ Share on other sites More sharing options...
brettpower Posted October 18, 2007 Share Posted October 18, 2007 Can you post the code that deals with the image upload/download? Quote Link to comment https://forums.phpfreaks.com/topic/73834-solved-dynamically-changing-script-extension/#findComment-372527 Share on other sites More sharing options...
Mirkules Posted October 18, 2007 Author Share Posted October 18, 2007 Thanks for your reply. There's not much to the code (assume the file is called image.php, and that the function imageSmoothCircle creates a smoothed circle): $img = imageCreateTrueColor( 320, 240 ); imageSmoothCircle( $img, 160, 120, 100, array( 'R' => 0xCC, 'G' => 0x33, 'B' => 0x00 ) ); ... header( 'Content-Type: image/png' ); imagePNG( $img ); imagedestroy($img); The question is how could I get the user to download this with a .png extension (for example) instead of the .php extension? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/73834-solved-dynamically-changing-script-extension/#findComment-372534 Share on other sites More sharing options...
BlueSkyIS Posted October 18, 2007 Share Posted October 18, 2007 add a header that sends the file name, i think it's content-disposition. Quote Link to comment https://forums.phpfreaks.com/topic/73834-solved-dynamically-changing-script-extension/#findComment-372541 Share on other sites More sharing options...
Mirkules Posted October 18, 2007 Author Share Posted October 18, 2007 Well, to reply to my own post, I finally figured it out (from php.net): If you want the user to be prompted to save the data you are sending, such as a generated PDF file, you can use the » Content-Disposition header to supply a recommended filename and force the browser to display the save dialog. <?php // We'll be outputting a PDF header('Content-type: application/pdf'); // It will be called downloaded.pdf header('Content-Disposition: attachment; filename="downloaded.pdf"'); // The PDF source is in original.pdf readfile('original.pdf'); ?> To apply this to my example above, it's impossible for the user to right-click on the image and save it as a .jpeg. BUT, if I wanted to push the image, or an excel document, or a text document to the browser's save as dialog, I could use the content-disposition header. Mirkules. Quote Link to comment https://forums.phpfreaks.com/topic/73834-solved-dynamically-changing-script-extension/#findComment-372544 Share on other sites More sharing options...
Mirkules Posted October 18, 2007 Author Share Posted October 18, 2007 One last note, you CAN control what the filename is when you right click on the image by omitting the attachment portion of the content-disposition. Quote Link to comment https://forums.phpfreaks.com/topic/73834-solved-dynamically-changing-script-extension/#findComment-372673 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.