Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  1. My Desktop Client sends a POST request to my PHP Script.
  2. The Script then calls mysqldump and generates a file - xdb_backup.sql
  3. 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, so 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. :)

like image 306
Jeff Avatar asked Jul 06 '11 20:07

Jeff


3 Answers

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.

like image 161
user482594 Avatar answered Oct 01 '22 19:10

user482594


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.

like image 25
datasage Avatar answered Oct 01 '22 21:10

datasage


You can set the Content-Length header with the size of xdb_backup.sql

like image 40
Jody Avatar answered Oct 01 '22 20:10

Jody