payney Posted October 30, 2007 Share Posted October 30, 2007 This is such a technical php question, but does anyone know how to do the following. I simply want to post a link on my site, which will allow the user to download a php file. Without it open in the page, so when the user clicks on it, it says open/save etc. Is this possible? This just opens the page: <a href="un_template.php">Download Template </a> Quote Link to comment https://forums.phpfreaks.com/topic/75368-solved-download-a-php-file/ Share on other sites More sharing options...
cooldude832 Posted October 30, 2007 Share Posted October 30, 2007 mod the headers so that it sets it as a download not a view. (look it up on php.net as I don't know the specifics) Quote Link to comment https://forums.phpfreaks.com/topic/75368-solved-download-a-php-file/#findComment-381193 Share on other sites More sharing options...
jester626 Posted October 30, 2007 Share Posted October 30, 2007 You could simply save the file as .txt or .phps and that will display the source code in the browser then the viewer could simply use the "save as" feature of the browser. Quote Link to comment https://forums.phpfreaks.com/topic/75368-solved-download-a-php-file/#findComment-381207 Share on other sites More sharing options...
payney Posted October 30, 2007 Author Share Posted October 30, 2007 Found a cracking solution: Create a new php page: <?php header('Content-disposition: attachment; filename=un_template.php'); header('Content-type: disposition'); readfile('un_template.php'); ?> Simply point your hyperlink to this. Quote Link to comment https://forums.phpfreaks.com/topic/75368-solved-download-a-php-file/#findComment-381209 Share on other sites More sharing options...
Dragen Posted October 30, 2007 Share Posted October 30, 2007 <?php $filesize = filesize($file); header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header("Content-Disposition: attachment; filename=".basename($file)); header("Content-Description: File Transfer"); header('Accept-Ranges: bytes'); header('Content-Length: ' . $filesize); readfile($file); ?> Where $file is the url to the wanted .php file. EDIT: note, if you set the filesize like in my example, your browser can correctly estimate download times. Also you should use application/force-download, to ensurw that it is downloaded. Use GET to set the $file. Quote Link to comment https://forums.phpfreaks.com/topic/75368-solved-download-a-php-file/#findComment-381213 Share on other sites More sharing options...
atlanta Posted October 30, 2007 Share Posted October 30, 2007 I think using get to call the file is a bad idea unless you filter the input, Quote Link to comment https://forums.phpfreaks.com/topic/75368-solved-download-a-php-file/#findComment-381219 Share on other sites More sharing options...
Dragen Posted October 30, 2007 Share Posted October 30, 2007 I think using get to call the file is a bad idea unless you filter the input, Obviously you'd filter it. But it makes it easier, if you've got lots of files just to send a request to one file which reads the GET value, instead of having to use several different files for each download. Most sites use GET for downloads, filter the input to make sure it's valid, not harmful etc. Quote Link to comment https://forums.phpfreaks.com/topic/75368-solved-download-a-php-file/#findComment-381223 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.