PHP file download for dynamic data
Sometime we ask visitors to download files from a web site. In this site also you can download many codes in zip format. We can ask the visitor to download the file and point one simple link to the zip file. On clicking the link the visitorís browser will show a window to save the zip file in client machine. But the problem comes when we donít have a file or a fixed URL to create a link. Say if a member is downloading his / her address book in CSV format then the records of this file is created on the fly. The records of the member is taken out from a table and formatted as per CSV file format and then the visitor / member gets a prompt to save the CSV file in the client machine. How to do this ?
Here by using the header option we will tell the browser that the file format is different then what it opens. We can set a default file name for the file also. Here is the code to do this. Note that in the second line we are specifying the default file name. In the third and fourth line we are storing the data and sending it to the client machine through the file download.
Note: Don't keep any html code or any other data before the header is sent to the browser, if any data goes then we will get warning message
header("Content-Disposition: attachment; filename=\"my-data.csv\"");
$data="col1, col start and end col2,col3, \n";
$data .= "seond linedata here from site to download col1";
Above code will display a window in visitor browser asking to save file in local computer or open by suitable application.
Read how records of a database table is downloaded as .csv file to local computer
Number of User Comments : 6
|Click here for More on PHP File functions|
|Excellent tutorial....thanks a lot.|
|how can i store .csv data in listbox by using php|
|good tutorials exciting to learn|
|please php script to display doc. file not image e g microsft word |
The code below works on my localhost to download a file. However, on top of my downloaded file there are 160 lines of HTML tags. How can I eliminate these tags and just get the plain file?
I appreciate your advice.
$fd = basename($csv);
header("Content-Disposition: attachment; filename="".$fd.""");
|how to create download from server exact code.|
and how to create file read, file write, file append exact code.