PHP miniwebsever file download

Posted by snikolov on Stack Overflow See other posts from Stack Overflow or by snikolov
Published on 2010-05-31T09:05:07Z Indexed on 2010/05/31 10:03 UTC
Read the original article Hit count: 266

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);
    $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";
      function openfile()
      {
      $filename = "a.pl";
      $file     = fopen($filename, 'r');
      $filesize = filesize($filename);
      $buffer   = fread($file, $filesize);
      $array    = array("Output"=>$buffer,"filesize"=>$filesize,"filename"=>$filename);
      return $array; 
      }

      $send   = openfile();
      $file   = $send['filename'];
      $filesize  = $send['filesize'];
      $output  = 'HTTP/1.0 200 OK\r\n';
      $output .= "Content-type: application/octet-stream\r\n";
      $output .= 'Content-Disposition: attachment; filename="'.$file.'"\r\n';
      $output .= "Content-Length:$filesize\r\n";
      $output .= "Accept-Ranges: bytes\r\n";
      $output .= "Cache-Control: private\n\n";
      $output .= $send['Output'];
      $output .= "Content-Transfer-Encoding: binary";
      $output .= "Connection: Keep-Alive\r\n";

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


 

Hello, I am snikolov i am creating a miniwebserver with php and i would like to know how i can send the client a file to download with his browser such as firefox or internet explore i am sending a file to the user to download via sockets, but the cleint is not getting the filename and the information to download can you please help me here,if i declare the file again i get this error in my server
Fatal error: Cannot redeclare openfile() (previously declared in C:\User s\fsfdsf\sfdsfsdf\httpd.php:31) in C:\Users\hfghfgh\hfghg\httpd.php on li ne 29
, if its possible, i would like to know if the webserver can show much banwdidth the user request via sockets, perl has the same option as php but its more hardcore than php i dont understand much about perl, i even saw that a miniwebserver can show much the client user pulls from the server would it be possible that you can assist me with this coding, i much aprreciate it thank you guys.

© Stack Overflow or respective owner

Related posts about php

Related posts about sockets