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

时间:2015-04-21 13:52:25

标签: php http-headers filenames

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

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

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

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

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

完整代码:

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 ();

6 个答案:

答案 0 :(得分:0)

尝试使用以下代码,它适合我

$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 ();

答案 1 :(得分:0)

请使用如下,我正在使用它。

        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使用您的文件格式。它适用于Microsoft Excel。

答案 2 :(得分:0)

试试这个:

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

答案 3 :(得分:0)

我没有看到代码中指定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;
}

答案 4 :(得分:0)

下面是一个.zip文件的片段,其中包含一些标题所需的信息描述,也许一个好的做法是在编写标题之前获取信息和一些验证。 还包括另一个较短的例子,

// 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;
}

&GT;

希望这有帮助!

答案 5 :(得分:-1)

只需像这样正确地逃脱您的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');