PHP:如何将mysql保存到excel文件到一个目录,而不是提示用户下载

我有一个脚本,将采取我的SQL语句,并转换为Excel。 我想将excel文件保存在一个文件夹中而不是提示下载,因为我想在excel文件上工作。

我的脚本

<?php /* Connecting, selecting database */ $db_link = mysql_connect("localhost", "root", ""); if (!$db_link) { die("Could not connect: " . mysql_error()); } mysql_select_db("mydb") or die("Could not select database"); /* Performing SQL query */ $sQuery="select * from comments where `comment` like '%apple%';"; $rResult = mysql_query( $sQuery) or die(); $count = mysql_num_fields($rResult); $html = '<table border="1"><thead><tr>%s</tr><thead><tbody>%s</tbody></table>'; $thead = ''; $tbody = ''; $line = '<tr>%s</tr>'; for ($i = 0; $i < $count; $i++){ $thead .= sprintf('<th>%s</th>',mysql_field_name($rResult, $i)); } while(false !== ($row = mysql_fetch_row($rResult))){ $trow = ''; foreach($row as $value){ $trow .= sprintf('<td>%s</td>', $value); } $tbody .= sprintf($line, $trow); } header("Content-type: application/vnd.ms-excel; name='excel'"); header("Content-Disposition: attachment; filename=exportfile.xls"); header("Pragma: no-cache"); header("Expires: 0"); print sprintf($html, $thead, $tbody); exit; ?> 

只要删除文件下载标题并保存文件

 /* header("Content-type: application/vnd.ms-excel; name='excel'"); header("Content-Disposition: attachment; filename=exportfile.xls"); header("Pragma: no-cache"); header("Expires: 0"); print sprintf($html, $thead, $tbody); exit; */ file_put_contents("exportfile.xls", $html.$thead.$tbody); // make sure that write permissions are set