2010-05-31 51 views
0

PHP miniwebsever文件下载

$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:\用户\ hfghfgh \ hfghg \ httpd.php李 NE
,如果可能的话,我想知道,如果网络服务器能表现出多大的banwdidth通过套接字的用户请求,Perl有相同的选项作为PHP,但它的核心比PHP我更了解perl,我甚至看到miniwebserver可以显示很多客户端用户从服务器上拉出来,有可能你可以用这种编码来帮助我,我非常赞赏它谢谢家伙。

+4

你设法把整个后一句话;) – 2010-05-31 09:59:12

回答

2

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

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

看到:http://en.wikipedia.org/wiki/List_of_HTTP_headers

+0

我已经拿到了filedownloading没有任何错误!我想知道它将如何能够查看带宽时cleint下载文件howmuch流发送给客户我真的很感谢! – Saxtor 2010-05-31 09:43:10

+0

你想看看那个?我不明白这个问题。您不需要立即发送整个区块。只需将它分成大块并逐个发送,并在您转移所有内容时关闭连接。然后,您可以在每个区块之后报告上传的内容。 – 2010-05-31 10:43:05

+0

嗯,接受一个答案,而不是upvoting它: -/ – 2010-07-28 07:32:24

1

每次你运行你的while (1)循环声明openfile功能。你只能声明一次函数。尝试在循环外移动openfile声明。

+0

它的作品!谢谢 – Saxtor 2010-05-31 09:57:47

+0

我得到这个问题,请帮助!,当我下载文件时,文件的长度是未指定的,我使用wget,它声明所以包括网络下载管理器 – Saxtor 2010-05-31 10:00:52

+0

$ 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;文件名=“”。$文件 '”\ r \ n'; $ output。=“Accept-Ranges:bytes \ r \ n”; $ output。=“Cache-Control:private \ n \ n”; $ output。= $ send ['Output']; $ output。=“Pragma:private \ n \ n”; – Saxtor 2010-05-31 10:04:37

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