PHP MINISERVER DOWNLOAD RESUME-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 11:02 UTC
Read the original article Hit count: 288

Filed under:
|
|
|
  $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 have 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 also i cant resume the download,i would like to know how i can monitor record the client output how much bandwidth he has downloaded perl has something like this put its hardcore if possible kindly provide me with some pointers thank you :)

© Stack Overflow or respective owner

Related posts about php

Related posts about sockets