Gucci Koo
Gucci Koo

Reputation: 571

PHP : how to post a 3 gigabyte chars long string by curl or output it in browser?

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();
}

Upvotes: 0

Views: 497

Answers (1)

TRD
TRD

Reputation: 1027

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.

Upvotes: 3

Related Questions