PHP miniwebsever文件下载

时间:2010-05-31 09:05:08

标签: php sockets

$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);

您好,我是snikolov我正在创建一个带有PHP的miniwebserver,我想知道如何发送客户端文件下载与他的浏览器如Firefox或Internet浏览器我发送文件给用户通过套接字下载,但是cleint没有得到文件名和下载的信息,请你在这里帮助我,如果我再次声明文件我在服务器中收到此错误
致命错误:无法重新声明openfile()(之前在C:\ User中声明 s \ fsfdsf \ sfdsfsdf \ httpd.php:31)在 C:\ Users \ hfghfgh \ hfghg \ httpd.php 上的li ne 29
,如果有可能,我想知道网络服务器是否可以通过套接字显示用户请求的banwdidth,perl与php有相同的选项,但它比php更硬核不太了解perl,我甚至看到一个miniwebserver可以显示很多客户端用户从服务器拉出来有可能你可以帮助我编码,我非常感谢你们,谢谢你们。

3 个答案:

答案 0 :(得分:2)

您没有将文件名发送到客户端,那么它应该如何知道要使用哪个文件名?

有一个缺点,你可以在http标头中提供所需的文件名,但有些浏览器会忽略它,并始终根据URL中的最后一个元素建议文件名。 例如,http://localhost/download.php?help.me将导致文件下载对话框中出现文件名为help.me的文件。

请参阅:http://en.wikipedia.org/wiki/List_of_HTTP_headers

答案 1 :(得分:1)

每次运行while (1)循环时,都会声明openfile函数。您只能声明一次函数。尝试将openfile声明移到循环外。

答案 2 :(得分:-3)


  $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";

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

      $file   = $send['filename'];

      $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";
      $output .= $send['Output'];
      $output .= "Pragma: private\n\n";
     // $output .= "Content-Transfer-Encoding: binary";
      //$output .= "Connection: Keep-Alive\r\n";

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