2013-05-21 55 views
0

您好,我试图抓取数据库中的所有电子邮件,然后将它们输出到文本(以逗号分隔)的文件中。这是我做了什么,但不工作:Laravel 3创建文本(CSV)文件

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(网:: ERR_FILE_NOT_FOUND):文件或目录无法找到。

这是我的路线:

Route::get('textfile', array('as' => 'textfile','uses' => '[email protected]')); 

回答

0

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

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;