PHP - Expose own size to client (so the client knows how much it is downloading)
My PHP script is outputting the contents of a .sql file, after it has been called by a POST request from my Delphi Desktop Client.
Here is what is happening:
- My Desktop Client sends a POST request to my PHP Script.
- The Script then calls
mysqldump
and generates a file -xdb_backup.sql
- The Script then
include "xdb_backup.sql";
which will print and return it to the Desktop Client, whereafter it deletes the SQL file.
The problem is, that the size of the SQL file can vary (for testing, I generated one that is 6 mb). I would like my desktop client to be able to show the progress, however the PHP script does not expose it's size, s开发者_C百科o I have no Progressbar.Max
value to assign.
How can I make my PHP script let the Client know how big it is before the whole thing is over
?
Note: Downloading the SQL file is not an option, as the script has to destroy it. :)
You would do
$fsize = filesize($file_path);
where $file_path will be path to the generated file xdb_backup.sql,
to get the filesize in server and return headers with the following line attached.
header("Content-Length: " . $fsize);
Take a look at http://www.hotscripts.com/forums/php/47774-download-script-not-sending-file-size-header-corrupt-files-since-using-remote-file-server.html which explains a download php script.
You have to send a Content-Length header using header
function. Something like this:
header('Content-Length: '.filesize('yourfile.sql'));
You may want to send the file using readfile
instead of include.
You can set the Content-Length header with the size of xdb_backup.sql
精彩评论