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?
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 1 of 1 people found this answer helpful. Did you? Yes No
you can use this code:
header('Content-type: text/plain'); header('Content-disposition: attachment; filename=hello.txt'); echo "Hello world"; Posted: MacOS 2 of 2 people found this answer helpful. Did you? Yes No |
© Advanced Web Core. All rights reserved