Have an account? Sign in
Login  Register  Facebook
How i can create a file on-the-fly and download it
I want to create a file on-the-fly and send it to the browser instead of the php site. I think I need to use the Header-Function, but how do I do that? Can anyone tell me that?
Started: September 18, 2011 Latest Activity: September 18, 2011 php header file
2 Answers
If you need to make some data to be downloaded as regular file then you need to say to Browser that the content you gonna send must be downloaded instead as displayed on the browser as a regular HTML. To to do that you can use the header. For example: if you need to get a .csv file dinamicly generated by PHP then you need to insert this lines of code before display any data on the page:
    header("Content-type: application/csv");
    header("Content-Disposition: attachment; filename=report.csv");
    header("Pragma: no-cache");
    header("Expires: 0");
    echo 'data1,data2,data3...';
Make sure you don't send any data to browser before setting any header parameter.

Posted: xtremex
In: September 18, 2011

you can use this code:
header('Content-type: text/plain');
header('Content-disposition: attachment; filename=hello.txt');
echo "Hello world";

Posted: MacOS
In: September 18, 2011

Your Answer

xDo you want to answer this question? Please login or create an account to post your answer