0

I'm doing an online database dumping tool.

But when I output the data, PHP will wait until it can calculate the length it needs to dump, which may confuse the user about whether the API is working or not.

How can I send a response in chunks?

I tried adding:

header('Transfer-Encoding:chunked');

But Chrome browser couldn't open the page with it.

What do I need to do?

Thanks!


Answer: should encoding data before send it.

function chunk_encoding($chunk) {
    printf("%x\r\n%s\r\n", strlen($chunk), $chunk);
    flush();
    ob_flush();
}
Gucci Koo
  • 541
  • 4
  • 10
  • Out heavy work in background and show preloader or something more dynamic in browser. It's first thing in my mind. – Leri Jun 28 '12 at 08:47
  • 1
    Which fun experience this user will have loading a 3 gb string into the browser window... What's the point in actually showing this data? Make it available for download – dan-lee Jun 28 '12 at 09:03
  • 1
    You must output something and then use function FLUSH() – Liutas Jun 28 '12 at 09:15
  • 1
    Have you considered dumping your DB to file, gzip and sending it like described here: http://stackoverflow.com/a/9458908/515124 – rodneyrehm Jun 28 '12 at 09:42

1 Answers1

3

It isn't the smartes approach to flood the user with 3GB of data. Assuming that this user has DSL connection (let's say 6MBit) he has to wait horrible ~69 minutes until all data can be used (e.g copied). In addition he isn't allowed to close the tab in which this data is loaded otherwise the data is lost. And finally any browser will grow to a memory consuming monster if he is forced to display this amount of data.

A better solution is to generate a file on the server and let the user download this file by showing him the link. This way the user can download the file in background (may be with any download manager) and can retrieve the data locally.

TRD
  • 1,027
  • 11
  • 20
  • 1
    Also the browser may (most likely: *will*) crash while trying to display this data. Loading 3 GB into the ram isn't very cheap (also think of the other 100 tabs and programs which may be opened). – dan-lee Jun 28 '12 at 09:42
  • `Content-Disposition:attachment; filename="db.sql"` i'll add attachment header in release version in debug mode the data size won't large than 100M but i still have the problem – Gucci Koo Jun 28 '12 at 09:55