在firefox bug中下载csv文件

时间:2014-07-23 11:04:18

标签: php codeigniter google-chrome firefox csv

我目前的CodeIgniter项目中有CSV下载功能。当用户单击该链接时,它将以CSV格式下载文件。一旦提供的代码中的文件名具有问号字符,下载的CSV文件的文件名就不正确。为了清楚起见,下面是代码。

public function download($role = NULL, $id = 0, $eid = 0) {
        $this->load->helper('download');
        $list = $this->respondent->get_respondents($eid);
        $questions = $this->respondent->get_all_questions($eid);

        $fp = fopen('php://output', 'w');
        $fields = array(
            'Timestamp',
            'First Name',
            'Middle Name',
            'Last Name',
            );

        $fields = array_merge($fields, $questions);

        fputcsv($fp, $fields);

    foreach ($list as $respondent) {
            $respondent_data = array(
            $respondent->since,
            $respondent->first_name,
            $respondent->middle_name,
            $respondent->last_name,
            );

            $query = $this->respondent->get_responses($respondent->rid);

            foreach($query as $response) {
            array_push($respondent_data, $response->answer, $response->duration);
        }

        fputcsv($fp, $respondent_data);
    }

    $data = file_get_contents('php://output');
    $name = $this->respondent->get_experiment($respondent->eid)->title.'.csv';

    // Build the headers to push out the file properly.
    header('Pragma: public');     // required
    header('Expires: 0');         // no cache
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Cache-Control: private',false);
    header('Content-Disposition: attachment; filename="'.basename($name).'"');
    header('Content-Transfer-Encoding: binary');
    header('Connection: close');
    exit();

    force_download($name, $data);
    fclose($fp);
    }

header('Content-Disposition: attachment; filename="'.basename($name).'"');行中,$name有问号字符后,在Firefox或Chrome浏览器中下载文件无法正确解释。生成的字符不是问号字符,而是分别是下划线或连字符。但是,当使用Safari进行下载时,文件名就可以了。是代码还是浏览器中的问题?

1 个答案:

答案 0 :(得分:0)

我在评论中的意思是this

str_replace('?', '_', $name); //when saving file on the server

这种方式对用户没有限制,只需将每个问号替换为_,我想没什么大不了的。

关于FF和GCH的部分我无法回答。