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?
+3
A:
<?php
header('Content-type: text/plain');
header('Content-disposition: attachment; filename=hello.txt');
echo "Hello world";
?>
Sjoerd
2010-06-11 08:23:10
this is the exactly i mean
moustafa
2010-06-11 08:26:18
@moustafa: Then acccept the answer. ;)
Bobby
2010-06-11 08:47:14
+1
A:
It's very, very difficult to understand what you're saying — what I think you're saying is you want a PHP script to make a client download its output instead of displaying it without creating a file. If that's what you're trying to do, you can use this code:
header("Content-Disposition: attachment; filename=my_file.txt");
icktoofay
2010-06-11 08:23:17
+1
A:
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.
Octan
2010-06-11 08:46:21