glenelkins Posted February 27, 2006 Share Posted February 27, 2006 How would i let a user click a link and download a file in php. This can be any file type Quote Link to comment https://forums.phpfreaks.com/topic/3663-download-with-code/ Share on other sites More sharing options...
tim_perrett Posted February 27, 2006 Share Posted February 27, 2006 Hi,try this... just set the content dispositsion and the file name etc[code]<?php function downloadFile($thefile, $ctype, $dname) { header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-type: ".$ctype); header("Content-Length: ".filesize($thefile)); header("Content-Disposition: attachment; filename=\"".$dname."\""); header("Content-Transfer-Encoding: binary"); $fp = fopen($thefile); fpassthru($fp); fclose($fp); }$thefile = "HSC21.pdf";$ctype = "application/pdf";$dname = "HSC21.pdf";downloadFile($thefile, $ctype, $dname);?>[/code]CheersTim Perrett Quote Link to comment https://forums.phpfreaks.com/topic/3663-download-with-code/#findComment-12692 Share on other sites More sharing options...
glenelkins Posted February 27, 2006 Author Share Posted February 27, 2006 ok cheers. is there a function to test a files extension? as i am wanting to allow downloads of multiple file types so:[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]$ctype = "application/pdf";[/quote]wot quite be good enough Quote Link to comment https://forums.phpfreaks.com/topic/3663-download-with-code/#findComment-12695 Share on other sites More sharing options...
glenelkins Posted February 27, 2006 Author Share Posted February 27, 2006 for some reason its not working. i pass the correct file name but it just seems to only want to download user.php but without the extension Quote Link to comment https://forums.phpfreaks.com/topic/3663-download-with-code/#findComment-12702 Share on other sites More sharing options...
tim_perrett Posted June 26, 2006 Share Posted June 26, 2006 im sure you can look at that code and mash somthing togehter to work out the content disposistion using the end(explode()) methods maybeIm not going to do it for you, thats not the name of game! lolTim Quote Link to comment https://forums.phpfreaks.com/topic/3663-download-with-code/#findComment-49681 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.