php - Sending hude csv file directly to browser output does not work -



php - Sending hude csv file directly to browser output does not work -

since ms excel not able recognize csv files of utf-8 encoding tried manually create such file prepending bom prefix. in case need send user/browser open rather save on disk. trick little amount of info ~1-10 works well, yet when of bigger number of records out, fails output in browser (google chrome, ff), rather prints result html. might seek play varying count parameter in request: http://tarex.ru/index.php?r=user/pricelist&file=1&count=100

the code $limit = $_get['count'] ? $_get['count'] : 10; $filename= 'test.csv'; $out = fopen('php://output', 'w'); // open out in browser fwrite($out, "\xef\xbb\xbf"); // prepend bom $counter=0; foreach(assortment::model()->findall( 'measure_unit<>"" , price>0' ) $d) { $arr = array( $d->article2, $d->title, $d->oem, $d->make, $d->availability , $d->getprice(yii::app()->user->id), $d->getdiscountopt(yii::app()->user->id) ); fputcsv($out, $arr, ';'); //delimiter - ; if ($counter++ > $limit) break; } header('content-type: text/csv; charset=utf-8'); header('content-disposition: attachment; filename="' . $filename . '"'); fclose($out);

is there solution?

update

eventually i've done yii::app()->request->sendfile after turutosiya's suggestion.

$filename='test.csv'; $filepath = yii::app()->basepath . '/../files/'. $filename; $out = fopen($filepath, 'w'); // writing csv ... fwrite($out, "\xef\xbb\xbf"); // set bom @ origin of file fputcsv($out, $arr, ';'); foreach(assortment::model()->findall() $d) { $arr = array( $d->article2, $d->title, $d->oem, $d->make, $d->availability); fputcsv($out, $arr, ';'); // separator - ; } fclose($out); yii::app()->request->sendfile($filename, @file_get_contents($filepath));

i suggest utilize xsendfile() method big file downloading.

$limit = $_get['count'] ? $_get['count'] : 10; $filename = 'test.csv'; $filepath = '/path/to/xsendfile-enabled/dir/' . $filename; $out = fopen($filepath, 'w'); // // write csv ... // fclose($out); yii::app()->request->xsendfile(filepath, array( 'savename' => $filename ));

php excel csv output

Comments

Popular posts from this blog

php - Edges appear in image after resizing -

ios8 - iOS custom keyboard - preserve state between appearances -

Delphi change the assembly code of a running process -