Page 1 of 1
force a download (netscape/IE)rather than display
Posted: Wed Mar 10, 2004 8:33 am
by lkj
force a download (netscape/IE)rather than display
I'd appreciate any help on this topic - i'd like the browser to download the info to a file (save) rather than display on screen. Is there a way to compress the content on server that when sent to browser the browser can automatically detect and uncompress it -for the reduction in size of traffic?
Thank you.
lkj
Posted: Wed Mar 10, 2004 9:03 am
by JayBird
something like this
Code: Select all
<?
// Get the filename from the query string of the file we want to download
$fileName = $_GET["file"];
// The full path to our downloads directory
$fileDir = "/full/server/path/to/the/downloads/";
// Combine the filename and the path
$path = "$fileDir$fileName";
////////////////////////////////////////
/* Force browser to download the file */
////////////////////////////////////////
global $HTTP_USER_AGENT;
$file = basename($path);
$size = filesize($path);
header("Content-Type: application/octet-stream");
header("Content-Type: application/pdf");
header("Content-Length: $size");
// IE5.5 just downloads index.php if we don't do this
if(preg_match("/MSIE 5.5/", $HTTP_USER_AGENT)) {
header("Content-Disposition: filename=$file");
} else {
header("Content-Disposition: attachment; filename=$file");
}
header("Content-Transfer-Encoding: binary");
$fh = fopen($path, "r");
fpassthru($fh);
?>
You might need to change the MIME type too.
Mark
Posted: Wed Mar 10, 2004 9:09 am
by lkj
remember it's not the file per se on the server that i want to download- it's the query results in the print statement that i want the browser to download as a file rather than display on the screen.
Thx.
lkj
Posted: Wed Mar 10, 2004 9:13 am
by JayBird
download as a CSV or something?
Posted: Wed Mar 10, 2004 9:31 am
by The Monkey
My webhost gave me this script, but I don't know how to "compress" the file or whatever.
Code: Select all
<?php
//connect to the database
$db=mysql_connect ('localhost', '<username>', '<password>');
mysql_select_db ('<db>');
// retrieve the filename to download from the query string of the page
$file = $_GET['filename'];
// name all of the mysql_query vars variables
$ip = $_SERVER['REMOTE_ADDR'];
$date = date('Y-m-$');
//increment the download database
$sql = "INSERT INTO downloads SET file = '$file', ip = '$ip', date = '$date'";
mysql_query($sql);
// send the headers
header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=$file");
// Add the subdirectory, if any
$directory = $_GET['directory'];
if($directory) {
$file = $directory . '/' . $file;
}
// and output the contents of the file
readfile($file);
?>
Save it as download.php, and put it in the same directory as the files you want "forced dl" to. then link to the file as "download.php?filename=file.ext". If there is a subdirectory one lower then where download.php exists, then you can do it like this: "download.php?directory=music&filename=file.ext".
Hope this helps!
Edit: u guys posted a bunch of stuff whilst I was typing this, so I guess this script is obsolete.

Maybe it will help somebody else, though.
Posted: Wed Mar 10, 2004 9:39 am
by JayBird
To downlaod results as CSV, i do this
export_csv.php
Code: Select all
Header ("Content-type: text/csv");
//This is my code, change your queries and formatting
$query = "SELECT * FROM contacts ORDER BY company";
$result = mysql_query($query) or die(mysql_error());
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "".$line['id'].",".$line['company'].",".$line['name'].",".$line['position'].",".$line['address'].",".$line['notes'].",".$line['tel'].",".$line['direct_line'].",".$line['fax'].",".$line['mobile'].",".$line['email'].",".$line['web']."\n";
}
?>
Now here is the trick, when linking to the reults, you link will look like this
http://192.168.1.2/ocdaintranet/contact ... export.csv
notice after export_csv.php i have added export.csv, this fool the broswer into downloading a CSV and not the PHP file
Mark
Posted: Wed Mar 10, 2004 10:38 pm
by lkj
may be i didn't make it clear.
what i want to do is to rather than displaying the result of a DB query i'd like to force the browser (NetScape/IE)for the content (text) to be downloaded by presenting the file save/as dialog.
e.g.
result from query:
$res = 'this is a test';
echo ( $res);
so when the server returns this query result to the browser instead of displaying it on the screen i want the browser to present to user the file save/as dialog.
i think it's as easy as that?
Thx.
lkj
Posted: Wed Mar 10, 2004 10:49 pm
by markl999
Something like this ?
Code: Select all
<?php
ob_start();
echo 'this is a test';
$output = ob_get_contents();
ob_end_clean();
$filename = 'foobar.txt';
$fp = fopen($filename, 'w');
fputs($fp, $output);
fclose($fp);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$filename");
readfile($filename);
?>
Posted: Thu Mar 11, 2004 3:04 am
by JayBird
lkj wrote:may be i didn't make it clear.
what i want to do is to rather than displaying the result of a DB query i'd like to force the browser (NetScape/IE)for the content (text) to be downloaded by presenting the file save/as dialog.
e.g.
result from query:
$res = 'this is a test';
echo ( $res);
so when the server returns this query result to the browser instead of displaying it on the screen i want the browser to present to user the file save/as dialog.
i think it's as easy as that?
Thx.
lkj
That's what my second code example does
Mark
Posted: Thu Apr 15, 2004 12:50 pm
by jasonn
Here is the code that I have worked up from different examples that will take a query and download the results as a csv without writing anything to the server.
Code: Select all
<?php
$sql = "select DISTINCT Field1, Field2, Field3, Field4 from Table1 where DateMailed is NULL order by Field2";
$result = mysql_query($sql, $db) or die(mysql_error());
$delimiter = ",";
$quote = '"';
$csv = "";
while ($row = mysql_fetch_assoc($result)) {
$first = true;
foreach ($row as $field) {
if (!$first) $csv .= $delimiter;
$field = preg_replace("/"/", """, $field);
$csv .= $quote.$field.$quote;
$first = false;
}
$csv .= "\r\n";
}
if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE 5.5")) {
$att = "";
} else {
$att = " attachment;";
}
header ('Content-Type: application/octet-stream');
header ('Content-Type: text/plain');
header ('Content-Disposition: ' . $att . ' filename=exportcsv.csv');
header ('Content-Transfer-Encoding: binary');
echo $csv;
?>
This seems to work fine and let you name the file what you want. Just link to the downloadcsv.php file with this code in it like you would any file.
Hope this helps.
jasonn.