【问题标题】:CSV Download from PHP space issue从 PHP 空间问题下载 CSV
【发布时间】:2012-10-19 05:21:45
【问题描述】:

每当有一个名称为 space 的 CSV 文件(如 xyz abc.csv)时,我们都会面临通过 PHP 代码下载的问题,而其他类似的 CSV 文件中没有空间,可以正常工作并正常下载。

下面是我的下载代码:

$file_path = 'http://www.servername.com/subfolder/subfolder/';
$file = urlencode('Today new_18-10-2012_21-13-21.csv');

header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="'.$file.'"');
header('Pragma: no-cache');
readfile($file_path.$file);

当文件被下载时,它总是0 bytes,而原始文件的大小是93kb

对此问题的任何建议。

提前致谢!

【问题讨论】:

    标签: php csv download


    【解决方案1】:

    试试这个...你必须使用rawurlencode 而不是urlencode

    $file_path = 'http://www.servername.com/subfolder/subfolder/';
    
    //just filename, no need to save file with url encoded name!
    $file = 'Today new_18-10-2012_21-13-21.csv';
    
    header('Content-Type: application/csv');
    header('Content-Disposition: attachment; filename="' . $file . '"');
    header('Pragma: no-cache');
    
    //here comes url encoding
    readfile($file_path . rawurlencode($file));
    

    它对我来说很好用……

    【讨论】:

      【解决方案2】:

      使用此代码。它可能对你有帮助。但我认为主要问题是间距。

      $Filename = 'Today new_18-10-2012_21-13-21.csv'; 
      $path = "http://www.servername.com/subfolder/subfolder/";
      $file = $path.$filename;
      header("Cache-Control: public");
      header("Content-Description: File Transfer");
      header("Content-Disposition: attachment; filename=$filename");
      header("Content-Type: application/csv");
      header("Content-Transfer-Encoding: binary");
      readfile($file);
      

      【讨论】:

      • 功能 savedata1 应该做什么?内容类型错误。什么改进了你的代码?
      • 不需要savedata1。这是一个将您的数据保存在数据库中的功能。
      猜你喜欢
      • 2019-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-14
      • 1970-01-01
      • 1970-01-01
      • 2015-06-11
      • 2021-11-19
      相关资源
      最近更新 更多