fwrite($ fp,$ schema_insert)不起作用

时间:2013-03-15 10:09:26

标签: php mysql

我正在使用此代码从数据库中获取数据并下载xls文件,但它没有将我的实际数据写入工作表。我认为它不起作用。我应该在代码中添加更多内容吗?

 <?php 
include 'functions.php';
 ob_start();

?>
<?php
if($_POST['types'] == 'name') {  

    $query = "SELECT * FROM visitor_detail WHERE name='".$_POST['filter']."' ORDER BY id DESC ";  
}  
elseif($_POST['types'] == 'mobile') {  

    $query = "SELECT * FROM visitor_detail WHERE mobile='".$_POST['filter']."' ORDER BY id DESC ";  
} 
elseif($_POST['types'] == 'OccasionType') {  

    $query = "SELECT * FROM visitor_detail WHERE OccasionType='".$_POST['filter']."' ORDER BY id DESC ";  
} 

elseif($_POST['types'] == 'InquiryDate') {  
    $query="SELECT * FROM visitor_detail WHERE TodayDate between '".$_POST['From']."' and '".$_POST['TO']."' ORDER BY id DESC " ;  
}

 elseif($_REQUEST['types'] == 'OccasionDate') {  
    $query="SELECT * FROM visitor_detail WHERE date between '".$_POST['From']."' and '".$_POST['To']."' ORDER BY id DESC ";  
                            }
else {  
    $query = "SELECT * FROM visitor_detail ORDER BY id DESC ";  
    } 

$result = mysql_query($query); 

$sep = "\t"; //tabbed character 
$fp = fopen('database.xls', "w"); 
$schema_insert = ""; 
$schema_insert_rows = ""; 
//start of printing column names as names of MySQL fields

//start of adding column names as names of MySQL fields 
for ($i = 1; $i < mysql_num_fields($result); $i++) 
{ 
$schema_insert_rows.=mysql_field_name($result,$i) . "\t"; 
} 
$schema_insert_rows.="\n"; 
echo $schema_insert_rows; 
fwrite($fp, $schema_insert_rows); 
//end of adding column names

while($row = mysql_fetch_row($result))
{
$schema_insert = ""; 
for($j=1; $j<mysql_num_fields($result);$j++) 
{ 
if(!isset($row[$j])) 
$schema_insert .= "NULL".$sep; 
elseif ($row[$j] != "") 
$schema_insert .= strip_tags("$row[$j]").$sep;
else 
$schema_insert .= "".$sep; 
} 
$schema_insert = str_replace($sep."$", "", $schema_insert); 

//this corrects output in excel when table fields contain \n or \r 
//these two characters are now replaced with a space 

$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert); 
$schema_insert .= "\n"; 
//$schema_insert = (trim($schema_insert)); 
//print $schema_insert .= "\n"; 
//print "\n";

fwrite($fp,$schema_insert); }

fclose($fp);

header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename=my.xls');
header('Pragma: no-cache');

?>

2 个答案:

答案 0 :(得分:1)

两种误解:

  1. 也许该功能称为“下载Excel文件”,但这并不意味着您必须创建一个真实的文件。

  2. PHP不会奇怪地向浏览器发送您在服务器端创建的所有文件。

  3. 删除所有与文件相关的代码,只需更改:

    fwrite($fp,$schema_insert);
    

    ......进入这个:

    echo $schema_insert;
    

    另外,请在第一个echo之前移动这些行:

    header('Content-Type: application/xls');
    header('Content-Disposition: attachment; filename=my.xls');
    header('Pragma: no-cache');
    

    编辑尽管有几种提及XLS格式,但有问题的代码基本上会创建一个CSV文件并使用虚假的HTTP标头发送它以欺骗Excel打开它。正如Mark指出的那样,PHP有一个builtin function to create CSV files,所以你不必自己编写代码。此函数需要文件指针,但同样需要there's no need to actually create a file:您可以使用php://output作为文件名调用fopen(),所有内容都将自动发送到浏览器。

答案 1 :(得分:0)

也许服务器不允许你写文件。您只能查询数据库中的数据并将其格式化为表格,并使用标题函数进行下载。