用变量定义文件名的正确语法是什么?

What is the correct syntax for defining filename with variables?

关于下载文件和定义 header,我无法为我的文件分配动态 filename。使用以下代码时:

header("Content-Disposition: attachment; filename=test.csv");

生成了一个 test.csv 文件供下载。但是,如果我使用这个:

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

它生成一个 .php 文件。使用此方法也不会将 Content-Dispositionfilename 传递给 header.

完整代码:

session_start();
    $file =$_SESSION['csvf'];
    $filename = $file."_".date("Y-m-d_H-i",time());
    header ( "Content-type: text/csv" );
    header("Content-Disposition: attachment; filename=test.csv");
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    print($file);
    exit ();

正确的语法是什么?

编辑

建议后的工作代码

session_start ();
$file = $_SESSION ['csvf'];
$filename =date ( "Y-m-d_H-i", time () );
header ( "Content-type: text/csv" );
header ( "Content-Disposition: attachment; filename=".$filename );
header ( 'Expires: 0' );
header ( 'Cache-Control: must-revalidate' );
header ( 'Pragma: public' );
header ( 'Content-Length: ' . filesize ( $file ) );
print ($file) ;
exit ();

试试下面的代码,对我有用

$file =$_SESSION['csvf'];
$filename = $file."_".date("Y-m-d_H-i",time()).".csv";
header ( "Content-type: text/csv" );
header("Content-Disposition: attachment; filename=".$filename);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
print($file);
exit ();

请按如下方式使用,我正在使用这个。

        header("Content-Type:   application/vnd.ms-excel; charset=utf-8");
        header("Content-Disposition: attachment; filename=".$fileName);  
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

代替 application/vnd.ms-excel 使用您的文件 format.It 适用于 Microsoft Excel。

试试这个:

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=test.csv');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;

您可以在此处查看有关使用 PHP 下载文件的更多信息:

http://php.net/manual/en/function.readfile.php

我没有在您的代码中看到 example.csv 的路径,您需要提供 $file 的完整路径,即:

$mySession = $_SESSION['csvf'];
//since $_SESSION['csvf'] contains the actual data you cannot use it for filename
$filename = date("Y-m-d_H-i",time()).".csv"; 
//write $mySession contents to file. Make sure this folder is writable
file_put_contents("/home/site/csvfolder/$filename", $mySession); 
$file = "/home/site/csvfolder/$filename";

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-type: text/csv');
    header('Content-Disposition: attachment; filename='.$filename);
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);
    exit;
}

下面是一个 .zip 文件的片段,其中对 header 需要的一些信息进行了一些描述,也许一个好的做法是在编写 header 之前获取信息和一些验证。 还包括另一个较短的示例,

// defines filename, type, path, size and a reference to file (handler)
// to set as values for file header
$filename = $survey_id . '.zip';

// define path to the file to be get file size on the next line
$filepath = [path to file]. '/' . $filename;

// used by 'Content-length'
$filesize = filesize($filepath);

// using fopen to get a file handler, 'r' for read, 'b' for binary (zip file)
$file_pointer = fopen($filepath, 'rb'); 

// check if file exists
if(is_file($filepath))
{
  // valid file?
  if($filesize && $file_pointer)
  {
      // some required header information to describe file, see [docs][1]
      header("Pragma: public");
      header("Expires: 0");
      header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
      header("Cache-Control: public");
      header("Content-Description: zip file");
      header("Content-Type: application/zip");
      header("Content-Type: application/force-download");
      header("Content-Disposition: attachment; filename=" . $filename);
      header("Content-Transfer-Encoding: binary");
      header("Content-length: " . $filesize);
      fpassthru($file_pointer); 

      // close 
      fclose($file_pointer);
    }
}

此外,请查看 readfile(),下面是一个较短的片段

<?php
 $file = 'monkey.gif';

 if (file_exists($file)) {
   header('Content-Description: File Transfer');
   header('Content-Type: application/octet-stream');
   header('Content-Disposition: attachment;    filename='.basename($file));
   header('Expires: 0');
   header('Cache-Control: must-revalidate');
   header('Pragma: public');
   header('Content-Length: ' . filesize($file));
   readfile($file);
   exit;
}

?>

希望对您有所帮助!

像这样正确地转义你的 php:

header('Content-Disposition: attachment; filename=' . time() . '.export.csv');

此示例将在文件名前加上 UNIX 时间戳。当然,您可能会对以编程方式使用变量或内置 php 函数感到疯狂,但这只是示例。

这里有更多的例子。这个文件名会在运行这个php代码的时候产生。 2019-05-18-contacts-main-export-by-Garrick.csv

header('Content-Disposition: attachment; filename=' . date("Y-m-d") . '-contacts-main-export-by-' . $uidName . '.csv');