簡體   English   中英

使用變量作為文件名在 php 中不起作用

[英]Using variable as file name doesn't work in php

所以我有這個代碼:

$pcbestand = date('Y-m-d h:i:s A') . ".xlsx";

$file =   $pcbestand ;
header('Content-Disposition: attachment; filename=' . $file );
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Length: ' . filesize($file));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
readfile($pcbestand);

$pcbestand 使用當前日期和時間制作 en .xlsx 文件,所以我試圖將此名稱命名為我的另存為對話框,但這就是我得到的:

在此處輸入圖片說明

如您所見,缺少 .xlsx 部分。 我究竟做錯了什么? 請幫忙。

如果我下載文件,則擴展名為空。

問題可能是 Windows 文件名不能包含字符: 嘗試

$pcbestand = date('Y-m-d h_i_s A') . ".xlsx";

甚至

$pcbestand = date('Y_m_d_h_i_s_A') . ".xlsx";

保持一致並刪除空格(文件名中的空格通常也不好。)

除了在文件名中使用不允許的字符(如:必須刪除)之外,您還需要引用名稱或去掉所有空格:

header('Content-Disposition: attachment; filename="' . $file . '"' );

嘗試

$pcbestand = date('Y-m-d') .".xlsx";

插入的

$pcbestand = date('Y-m-d h:i:s A') . ".xlsx";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM