PHP file download for dynamic data

Sometime we ask visitors to download files from a web site. 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 the 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 ?
<?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;
?>

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

Above code will display a window in visitor browser asking to save file in local computer or open by suitable application.

Example: Secure File Download

session_start();
if (!isset($_SESSION['user'])) {
    die("Unauthorized access!");
}
$file = 'path/to/file.zip';
if (file_exists($file)) {
    header('Content-Type: application/zip');
    header('Content-Disposition: attachment; filename="'.basename($file).'"');
    header('Content-Length: ' . filesize($file));
    readfile($file);
    exit;
}

Example: Handling Large Files with Chunking

$file = 'large-file.zip';
$chunk_size = 8192; // 8KB chunks
$handle = fopen($file, 'rb');
while (!feof($handle)) {
    echo fread($handle, $chunk_size);
    flush();  // Send the data immediately
}
fclose($handle);
These examples demonstrate secure file handling, setting MIME types, and addressing large file downloads.
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



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.




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