2010-05-31 50 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);