Jump to content

download file without header() function


adza

Recommended Posts

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

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>

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...)

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...

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  ;D

 

- download.php

  - userdownloads (folder)

    - myfile.csv

 

that is the directory tree.

 

try it with those 3 files unchanged

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.