PHP - set your own size to the client (therefore, the client knows how much it loads)

My PHP script outputs the contents of the .sql file after it was called by a POST request from my Delphi Desktop client.

Here's what happens:

  • My desktop client sends a POST request to my PHP Script.
  • the script then calls mysqldump and generates a file - xdb_backup.sql
  • script, then include "xdb_backup.sql"; , which will print and return to its Desktop Client, and then deletes the SQL file.

The problem is that the size of the SQL file can vary (for testing, I generated a file with a size of 6 mb). I would like my desktop client to be able to show progress, however the PHP script does not disclose its size, so I am not assigned the value of Progressbar.Max .

How can I make my PHP script so that the client knows how big it is before the whole thing is over ?

Note. Downloading the SQL file is not an option, as the script should destroy it. :)

+6
source share
3 answers

Would you do

 $fsize = filesize($file_path); 

where $ file_path will be the path to the generated xdb_backup.sql file,

to get the file size on the server and return the headers with the next line.

 header("Content-Length: " . $fsize); 

Have 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 the download PHP script.

+10
source

You must send the Content-Length header using the header function. Something like that:

header('Content-Length: '.filesize('yourfile.sql'));

You can send the file using readfile instead of include.

+1
source

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

+1
source

Source: https://habr.com/ru/post/892169/


All Articles