SQL PHP HTML ASP JavaScript articles and free scripts to download
PHP File Function

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.
<?php

header("Content-type: application/octet-stream"); 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"; echo $data; ?>
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

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


Google+
Ramesh28-08-2009
Excellent tutorial....thanks a lot.
mukesh16-03-2010
how can i store .csv data in listbox by using php
kapil agrawal21-03-2010
good tutorials exciting to learn
raymond27-12-2010
please php script to display doc. file not image e g microsft word
Moe08-01-2011
Hello,

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-type: application/octet-stream");
header("Content-Disposition: attachment; filename="".$fd.""");
header("Content-Description: Download");
readfile($csv);
pappu chauhan16-11-2012
how to create download from server exact code.
and how to create file read, file write, file append exact code.
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked



HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer