file transfer from server to client

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'm trying to write a little php script to transfert some
files from a server to clients (web/http).

It's working fin with small files. But transfering big files
(try on 1Gb) failed! The transfert is stoped randomly (sometimes
at 25%, sometimes at 75%,...).

And I don't understand why?! :/

Here, a part of my php script:

  - Firstly, I'm sending the http headers

header('HTTP/1.1 200 OK');
header('Status: 200 OK');
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2,  
date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");

header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header('Accept-Ranges: bytes');
header('Content-Transfer-Encoding: Binary');
header('Content-Type: application/force-download');
//header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$filename.'"');
//header("Content-Disposition: inline; filename=$name");
header('Content-Length: '.$length);

  - After that, I have a loop to send the content of the file
    (and I'm writing in same time a debuf file on the server)

$fd = fopen("debug.txt", "a");
fwrite($fd, "start of transfert\n");

while(true) {
    if (connection_aborted()) {
             $fd = fopen("debug.txt", "a");
                 fwrite($fd, "connection aborted\n");
                 break ;
    } else if (connection_status() != 0) {
        $fd = fopen("debug.txt", "a");
                 fwrite($fd, "connection status\n");
                 break ;
    } else if (feof($fp)) {
        $fd = fopen("debug.txt", "a");
        fwrite($fd, "end of file\n");
        break ;
    } else {
        print(fread($fp, 1024*8));

$fd = fopen("debug.txt", "a");
fwrite($fd, "end of transfert\n");

So, the first thing I've noticed is that
  header('Content-Length: '.$length);
give the correct file size!
(wget my_script.php show the correct file size to be transfer).

The second thing is that the debug file contains only this:
"start of transfer
start of transfer
start of transfer
(The wget command re-start the download of the file when
the transfer is closed before the end and the wget command
write on the screen: "connection closed").

I'm trying to delete the 2 tests (connection_aborted() and  
connection_status()), but the same bug appears!
Identically bug when I'm trying to read the file not with
1024*8  block size, but with only 1024 size.

Someone has an genius idea for me?

Thanks (ans sorry for my bad english),


Re: file transfer from server to client

Alex wrote:

Quoted text here. Click to load it
Quoted text here. Click to load it

Most probably, your script is dying because it has used its 30 sec time
slice. You can try to feed a higher number to set_time_limit(), but I don't
recommend that.

For files that large, rely on the web server directly.

Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org- ; ;

Site Timeline