sac0o01 Posted January 9, 2012 Share Posted January 9, 2012 So I am trying to download an image created with my php script. I use the following for the download: <?php session_start(); $filename = $_SESSION['imgOut']; header('Content-type: application/octet-stream'); header('Content-Disposition: attachment; filename='.$filename ); ?> The file downloads however instead of downloading "example.jpg" from the /temp folder , it downloads "temp_example.jpg" which of course is an empty file. I am sure I am approaching this the wrong way, any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/254677-download-link-replacing-slashes-in-path-with-underscores/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 9, 2012 Share Posted January 9, 2012 The filename="..." attribute in the Content-Disposition header is just a string for the destination filename to use in the client. It is not the full path to where the file is on the server. Quote Link to comment https://forums.phpfreaks.com/topic/254677-download-link-replacing-slashes-in-path-with-underscores/#findComment-1305927 Share on other sites More sharing options...
sac0o01 Posted January 9, 2012 Author Share Posted January 9, 2012 I see that now...so now I have to figure out how to call the image up. If I echo $filename it returns the correct path i.e. temp/example.jpg How do I go about downloading the image? Right now I just tell users to right click and save but would like to have the download link or button. Quote Link to comment https://forums.phpfreaks.com/topic/254677-download-link-replacing-slashes-in-path-with-underscores/#findComment-1305938 Share on other sites More sharing options...
PFMaBiSmAd Posted January 9, 2012 Share Posted January 9, 2012 basename would give you just the filename to use in the Content-Disposition header attribute. Quote Link to comment https://forums.phpfreaks.com/topic/254677-download-link-replacing-slashes-in-path-with-underscores/#findComment-1305940 Share on other sites More sharing options...
Adam Posted January 9, 2012 Share Posted January 9, 2012 As for the actual file data, you need to quite literally echo the contents into the browser after you've sent the content-type header: echo file_get_contents($filename); Looking at the name of your session varibale ("imgOut"), is application/octet-stream really the right content-type to use here? Quote Link to comment https://forums.phpfreaks.com/topic/254677-download-link-replacing-slashes-in-path-with-underscores/#findComment-1305964 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.