darphas Posted October 13, 2012 Share Posted October 13, 2012 i'm making a online catalog. upload images to the server and save the nanem in the db like 92.jpg,93.jpg etc.. so i wanto to know how to make a button to download the images.. i tried to put the url of the image but only display it in the browser. so please help. Quote Link to comment https://forums.phpfreaks.com/topic/269422-button-to-download-images/ Share on other sites More sharing options...
3raser Posted October 13, 2012 Share Posted October 13, 2012 (edited) $path should be the path to the image you want the viewer to download. <?php //path to the picture you're wanting the viewer to download $path = 'picture.gif'; if(file_exists($path)){ //split the extension and name from eachother $e = explode('.', $path); //get the name of the file $file_name = $e[0]; //image extension $extension = $e[1]; // Send a header saying we'll be displaying a picture header('Content-type: image/'. $extension); // Name the file header('Content-Disposition: attachment; filename="'. $file_name .'.'. $extension .'"'); // Path to the picture you're wanting the user/viewer to download readfile($file_name .'.'. $extension); }else{ echo 'The requested image does not exist.'; } ?> Edited October 13, 2012 by 3raser Quote Link to comment https://forums.phpfreaks.com/topic/269422-button-to-download-images/#findComment-1384977 Share on other sites More sharing options...
darphas Posted October 13, 2012 Author Share Posted October 13, 2012 great brow.. and the link looks like? Quote Link to comment https://forums.phpfreaks.com/topic/269422-button-to-download-images/#findComment-1384988 Share on other sites More sharing options...
3raser Posted October 13, 2012 Share Posted October 13, 2012 Well, you can make a small modification to the $path variable: $path = trim($_GET['file']); Then, you'd simply link to it: <a href="download.php?file=picture.gif">This is a link. Download the image.</a> And of course download.php would be changed to whatever you named the PHP file I provided to you. Quote Link to comment https://forums.phpfreaks.com/topic/269422-button-to-download-images/#findComment-1384989 Share on other sites More sharing options...
darphas Posted October 13, 2012 Author Share Posted October 13, 2012 Thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/269422-button-to-download-images/#findComment-1384993 Share on other sites More sharing options...
PFMaBiSmAd Posted October 13, 2012 Share Posted October 13, 2012 You need to validate the supplied filename.ext in $path. As is stands now, anyone can use the script to output any file on your server, such as the one that contains your database connection details. Quote Link to comment https://forums.phpfreaks.com/topic/269422-button-to-download-images/#findComment-1385013 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.