Tuesday, 15 July 2014

php - Server returns broken file -



php - Server returns broken file -

i have script taking info db , collect php output cache (ob_start) printing it. after flushing , prompt save file. file not of needed length. have 160 kb length, have total length 15 mb. ran script 20 times , 3 times of 20 got normal file size, in other cases has same size (160 538 bytes).

why happen?

short variant of code:

ob_start(); $offset = 0; $count = 20000; while (true) { $datafrommysql = getmysqldata($count, $offset); foreach($datafrommysql $data) { print implode(';', $data); } if (!$datafrommysql || count($datafrommysql) < $count) { break; } $offset += $count; } header('cache-control: no-cache, no-store, must-revalidate'); header('pragma: no-cache'); header('expires: 0'); header('content-type: application/csv'); header('content-length: ' . ob_get_length()); header('content-disposition: attachment; filename="export.csv"'); ob_end_flush(); exit;

site stands on 2 physical servers. asked admin direct flow main server script.

instead of using ob_* functions write string variable, can utilize strlen() content-length , print out total string @ end.

edit: might more useful fputcsv(), utilize generate csv file server-side , send out @ end using readfile().

php nginx export attachment

No comments:

Post a Comment