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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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