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

File Deleting File Checking if file exists before deleting
PHP
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Ramesh

    28-08-2009

    Excellent tutorial....thanks a lot.
    mukesh

    16-03-2010

    how can i store .csv data in listbox by using php
    kapil agrawal

    21-03-2010

    good tutorials exciting to learn
    raymond

    27-12-2010

    please php script to display doc. file not image e g microsft word
    Moe

    08-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 chauhan

    16-11-2012

    how to create download from server exact code. and how to create file read, file write, file append exact code.

    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer