adza Posted October 12, 2007 Share Posted October 12, 2007 I am currently working on a little application that offers the user a chance to either view the results of their query on the screen or to download a .csv file. I was wondering how i can deliver the file to the browser without using the header() function as i have already outputted heaps of stuff to the screen (html headers, page divs for formatting etc etc)... Google always seems to point me to someone simply crowing about the header() function.. is there any other way to do this? Link to comment https://forums.phpfreaks.com/topic/72898-download-file-without-header-function/ Share on other sites More sharing options...
~n[EO]n~ Posted October 12, 2007 Share Posted October 12, 2007 Create a download.php file and put this code <?php $filepath="userdownloads/myfile.csv";; $filename="myfile.csv"; downloadFile($filename,$filepath); function downloadFile($filename,$filepath) { header("Content-type: application/pdf"); // instad of pdf use others... for text header("Content-disposition: attachment; filename=".$filename); header("Content-Length: " . filesize($filepath)); header("Pragma: no-cache"); header("Expires: 0"); readfile($filepath); return; } ?> and you can specify the link to download like this <a href="download.php"><img src="images/check1.jpg" alt="checking" width="173" height="38" border="0" /></a> Link to comment https://forums.phpfreaks.com/topic/72898-download-file-without-header-function/#findComment-367676 Share on other sites More sharing options...
adza Posted October 18, 2007 Author Share Posted October 18, 2007 thanks for the tip!! i did get around to getting this to work finally!!! haha, great learning curve this has been, and the funniest thing... forgot to start the session to get the session var of the filename!! :@ oh well, it's times like these that make you remember the simple stuff i guess (bashed my head against this wall for a while...) Link to comment https://forums.phpfreaks.com/topic/72898-download-file-without-header-function/#findComment-372199 Share on other sites More sharing options...
adza Posted October 18, 2007 Author Share Posted October 18, 2007 errr... check that... not working... ?? The filename is now downloaded as an empty file? this poses some questions for me, as the file exists and i can open it without problem from the 'server' (local) webroot directory. In that function, how should i be defining my filepath? I've tried './' (to signify the webroot dir - where the file is being saved at the moment), also tried /var/www/ and still can't get it to download a file with data??? This is quite confusing... Link to comment https://forums.phpfreaks.com/topic/72898-download-file-without-header-function/#findComment-372209 Share on other sites More sharing options...
mattal999 Posted October 18, 2007 Share Posted October 18, 2007 it the download.php in a folder and then the userdownloads/myfile in a folder called userdownloads in that folder... ??? confusing, let me re-phrase - download.php - userdownloads (folder) - myfile.csv that is the directory tree. try it with those 3 files unchanged Link to comment https://forums.phpfreaks.com/topic/72898-download-file-without-header-function/#findComment-372218 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.