0

您好,我正在尝试从数据库中获取所有电子邮件,然后将它们输出到文本(逗号分隔)文件中。这是我已经完成但不起作用的操作:

public function get_textfile() {

$emails = Staff::get('email');

header("Content-type: text/csv");  
header("Cache-Control: no-store, no-cache");  
header('Content-Disposition: attachment; filename="filename.txt"');

$stream = fopen("php://output", 'w');

foreach($emails as $email) {
fputcsv($stream, $email, ',');
}

fclose($outstream);     
}

return (something)?

得到这个:错误6(net :: ERR_FILE_NOT_FOUND):找不到文件或目录。

这是我的路线:

    Route::get('textfile', array('as' => 'textfile','uses' => 'admin@textfile'));
4

2 回答 2

0

将所有数据收集到一个字符串中,然后像这样输出:

$data = '';
foreach ($emails as $email)
{
    // If you want 1 email per line
    $data .= '"'.$email.'"'.PHP_EOL;

    // If you want all emails on 1 line
    $data .= '"'.$email.'",';
}

header('Content-type: text/csv');
header('Content-Disposition: attachment; filename=My Cool File.csv');
header('Pragma: no-cache');
header('Expires: 0');

echo $data;
于 2013-05-21T15:42:54.223 回答
0

尝试file_put_contents($filename, implode(',', Staff::get('email')));

于 2013-05-21T15:36:31.817 回答