我使用PHPExcel
创建了文件,说C:\path\goes\here\filename.xls
当我尝试使用header(),
访问它时,它会创建一个文件filename(1)
,filename(2)
,依此类推。
(PS:当我说覆盖时,我的意思是删除以前的内容并添加新数据)。
<?php
include 'PHPExcel.php';
require_once ("C:\\xampp\\htdocs\\excel_mailer_2\\PHPMailer-master\\class.phpmailer.php");
$path = "C:\\Users\\Manish\\Downloads";
$file_name = $_GET['file_name'];
$DB_server = "localhost";
$DB_username = "root";
$DB_password = "";
$DB_dbname = "timetracker";
$DB_tablename = "tt_users";
$conn = mysqli_connect ($DB_server, $DB_username, $DB_password, $DB_dbname);
if (mysqli_connect_errno()) {
die ("Connection Failed!");
}
$sql = "SELECT *";
$sql .= " FROM $DB_tablename";
if ($result = mysqli_query($conn,$sql));
else die("Couldn't execute query:<br />" . mysqli_error(). "<br />" . mysqli_errno());
$file_ending = ".xls";
$filename = $file_name.$file_ending;
//header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$filename");
/*******Start of Formatting for Excel*******/
$sep = "\t";
$flag = true;
$col_names = "";
while($row = mysqli_fetch_assoc($result)) {
$data_insert = "";
foreach($row as $field=>$data) {
if ($flag)
$col_names .= $field.$sep;
if(!isset($data))
$data_insert .= "NULL".$sep;
elseif ($data != "")
$data_insert .= $data.$sep;
else
$data_insert .= "".$sep;
}
if ($flag) {
$flag = !$flag;
$col_names .= "\t";
echo (trim($col_names));
echo "\n";
}
$data_insert .= "\t";
echo (trim($data_insert));
echo "\n";
}
?>
答案 0 :(得分:0)
您好使用此
在filename
函数中写下header
header('Content-Disposition: attachment; filename="filename.xls"');
答案 1 :(得分:0)
您可以使用 php basename() 函数获取该特定路径下的文件名,然后检查您正在创建的文件是否已经存在。 如果它已经存在,则删除旧文件并创建新文件。 https://www.w3schools.com/php/func_filesystem_basename.asp
这应该在你调用 header("Content-Disposition: attachment; filename=$filename"); 之前完成;