Re: php downlaod script

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Hulf,

Just looking at it, I'm not sure why it's not working. But here is the script I use for exporting database files to excel:

<?PHP


$sortOrder = $_SESSION['order'];
$search = $_SESSION['search'];
$select = "SELECT * FROM ".$table." WHERE FName like '%".$search."%' or LName like '%".$search."%' or Add1 like '%".$search."%' or Add2 like '%".$search."%' or City like '%".$search."%' or State like '%". $search."%' or Zip like '%".$search."%' or XCode like '%".$search."%' order by ".$sortOrder."";

$export = mysql_query($select);
$fields = mysql_num_fields($export);

for ($i = 0; $i < $fields; $i++) {
	$header .= mysql_field_name($export, $i) . "\t";
}

while($row = mysql_fetch_row($export)) {
	$line = '';
	foreach($row as $value) {
		if ((!isset($value)) or ($value == "")) {
			$value = "\t";
		}
		else
		{
			$value = str_replace('"', '""', $value);
			$value = '"' . $value . '"' . "\t";
		}	
		$line .= $value;
	}
	$data .= trim($line). "\n";
}
$data = str_replace("\r", "", $data);

if ($data =="") {
	$data ="\n(0) Records Found!\n";
}
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=Export.xls");
header("Pragma: no-cache");
header("Expires: 0");


print "$header\n$data";


?>

On Sep 25, 2007, at 9:22 AM, Hulf wrote:

My download script only gives me empty (0kb) xls files. They are being
uploaded as blobs and seem ok. Can someone shed some light on why the
downoad is not working.

Many Thanks,


R.


<?php
if(isset($_GET['id']))
{
// if id is set then get the file with the id from database

$id    = $_GET['id'];
$query = "SELECT id, file_name, size, type, content FROM results WHERE id =
'$id'";

$result = mysql_query($query) or die(mysql_error());
list($id, $file_name, $type, $size, $content) = mysql_fetch_array ($result);

header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$file_name");
header("Content-Length: ".filesize($size));
header("Accept-Ranges: bytes");
header("Pragma: no-cache");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-transfer-encoding: binary");


Just out of my own curiosity, What does the Content-Length and Accept- Ranges do?


exit;
}

?>


Also, when I was trying to get mine working, I commented out all the header calls so it would print the results on the page so I didn't end up with 500 excel spreadsheets on my desktop.
--

Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
3251 132nd ave
Holland, MI, 49424
www.raoset.com
japruim@xxxxxxxxxx

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux