PHP mini-server download resulme-error! Resource id # 4

Posted by snikolov on Stack Overflow See other posts from Stack Overflow or by snikolov
Published on 2010-05-31T11:00:10Z Indexed on 2010/05/31 12:23 UTC
Read the original article Hit count: 292

Filed under:
|
|
|
<?php
$httpsock = @socket_create_listen("9090");
if (!$httpsock) {
    print "Socket creation failed!\n";
    exit;
}
while (1) {
    $client = socket_accept($httpsock);
    $input = trim(socket_read ($client, 4096));

    $input = explode(" ", $input);
    $range = $input[12]; 
    $input = $input[1];

    $fileinfo = pathinfo($input);

    switch ($fileinfo['extension']) {
        default:
            $mime = "text/html";
    }
    if ($input == "/") {
        $input = "index.html";
    }
    $input = ".$input";
    if (file_exists($input) && is_readable($input)) {
        echo "Serving $input\n";
        $contents = file_get_contents($input);
        $output = "HTTP/1.0 200 OK\r\nServer: APatchyServer\r\nConnection: close\r\nContent-Type: $mime\r\n\r\n$contents";
    } else {
        //$contents = "The file you requested doesn't exist.  Sorry!";
        //$output = "HTTP/1.0 404 OBJECT NOT FOUND\r\nServer: BabyHTTP\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n$contents";

        if(isset($range)) {
            list($a, $range) = explode("=",$range);
            str_replace($range, "-", $range);
            $size2    = $size-1;
            $new_length = $size-$range;
            $output   = "HTTP/1.1 206 Partial Content\r\n";
            $output  .= "Content-Length: $new_length\r\n";
            $output  .= "Content-Range: bytes $range$size2/$size\r\n";
        }
        else {
            $size2=$size-1;
            $output  .= "Content-Length: $new_length\r\n";
        }

        $chunksize  = 1*(1024*1024);
        $bytes_send = 0;

        $file     = "a.mp3";
        $filesize = filesize($file);     
        if ($file = fopen($file, 'r')) {
            if(isset($range))
                $output  = 'HTTP/1.0 200 OK\r\n';
            $output .= "Content-type: application/octet-stream\r\n";
            $output .= "Content-Length: $filesize\r\n";
            $output .= 'Content-Disposition: attachment; filename="'.$file.'"\r\n';
            $output .= "Accept-Ranges: bytes\r\n";
            $output .= "Cache-Control: private\n\n";

            fseek($file, $range);

            $download_rate = 1000;
            while(!feof($file) and (connection_status()==0)) {
                $var_stat = fread($file, round($download_rate *1024));
                $output  .=  $var_stat;//echo($buffer); // is also possible
                flush();
                sleep(1);//// decrease download speed
            }
            fclose($file);
        }

        /**
         $filename = "dada";
         $file     = fopen($filename, 'r');
         $filesize = filesize($filename);
         $buffer   = fread($file, $filesize);
         $send     = array("Output"=>$buffer,"filesize"=>$filesize,"filename"=>$filename);

         $file   = $send['filename'];
         */
        //@ob_end_clean();

        // $output .= "Content-Transfer-Encoding: binary";
        //$output .= "Connection: Keep-Alive\r\n";

    }
    socket_write($client, $output);
    socket_close ($client);
}
socket_close ($httpsock);

Hey guys, I haved create a miniwebserver downloader. It can download files from your server. However, I am unable to resume my download when I download the file – I get Resource id # 4 – and I also can't resume the download.

I would like to know how I can monitor and record the client output and how much bandwidth he has downloaded. Perl has something like this, but it's hardcore; if possible, kindly provide me with some pointers thank you :)

© Stack Overflow or respective owner

Related posts about php

Related posts about sockets