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

안녕하세요, 나는 내가 PHP와 miniwebserver을 만드는 오전과 내가 클라이언트에게 내가 보내고 탐구 파이어 폭스 또는 인터넷으로 자신의 브라우저를 다운로드 할 수있는 파일을 보낼 수있는 방법을 알고 싶습니다 snikolov 사용자가 파일을 소켓을 통해 다운로드 할 수 있지만 클린턴 파일 이름 및 다운로드 할 정보를 얻지 못하고 있습니다. 만약 내가 파일을 다시 선언하면 내 서버에이 오류가 발생합니다.
치명적인 오류 : openfile() (이전에 C : \ User s \ fsfdsf \ sfdsfsdf \ httpd.php : 31에 선언 된)을 다시 선언 할 수 없습니다. C : 사용자 \ \ hfghfgh \ hfghg \ httpd.php는 리튬 네브라스카 29
, 그것의 가능한 경우, 내가 웹 서버는 소켓을 통해 사용자 요청 banwdidth 많이 보여줄 수 있는지 알고 싶습니다에, 펄 같은 옵션이 있습니다 하지만 PHP보다 더 많은 하드 코어는 펄에 대해 많이 이해하지 못한다. 심지어 미니 웹 서버는 클라이언트 사용자가 서버에서 가져온 것을 많이 보여줄 수 있다는 것을 알았다.이 코딩으로 나를 도와 줄 수 있었으면 좋겠다. 얘들 아.

+4

, 내가 cleint 파일을 다운로드 할 때 대역폭을 볼 수있을 것입니다 방법을 알고 싶습니다) –

답변

2

파일 이름을 클라이언트에 보내지 않으므로 사용할 파일 이름을 어떻게 알 수 있습니까?

단점이 있습니다. http 헤더에 원하는 파일 이름을 제공 할 수 있지만 일부 브라우저는이를 무시하고 URL의 마지막 요소를 기반으로 파일 이름을 제안합니다. 예를 들어 http://localhost/download.php?help.me을 입력하면 파일 다운로드 대화 상자에 도움이되는 파일 이름 help.me가 생성됩니다.

은 다음을 참조하십시오 http://en.wikipedia.org/wiki/List_of_HTTP_headers

+0

내가 어떤 오류없이 filedownloading를 가지고있다! howmuch 스트림 정말 감사하는 고객에게 보내집니다! – Saxtor

+0

어디에서 보시겠습니까? 나는 그 질문을 이해하지 못한다. 한 번에 전체 블록을 보낼 필요는 없습니다. 그냥 덩어리로 분할하고 하나씩 보내면 모든 것을 전송했을 때 연결을 닫습니다. 그런 다음 업로드 된 내용을 각 청크 뒤에보고 할 수 있습니다. –

+0

흠, 대답을 수락하고 그것을 upvoting하지 : -/ –

1

매번 당신은 당신의 while (1) 루프를 실행하면 openfile 기능을 선언합니다. 함수를 한 번만 선언 할 수 있습니다. 루프 밖에서 openfile 선언을 이동하십시오.

+0

그것은 작동합니다! 덕분에 – Saxtor

+0

나는이 문제를 도와주고있다!, 내가 파일을 다운로드 할 때 파일의 길이가 지정되지 않은, 나는 wget을 사용하고 있으므로 인터넷 다운로드 관리자를 포함한다. – Saxtor

+0

$ output = 'HTTP/1.0 200 OK \ r \ n' ; $ output. = "콘텐츠 유형 : application/octet-stream \ r \ n"; $ output. = "Content-Length : $ filesize \ r \ n"; $ output. = '콘텐츠 - 처리 : 첨부 파일; filename = " '. $ file.'"\ r \ n '; $ output. = "Accept-Ranges : bytes \ r \ n"; $ output. = "캐시 제어 : 개인 \ n \ n"; $ output. = $ send [ '출력']; $ output. = "Pragma : private \ n \ n"; – Saxtor

-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); 
당신은 한 문장으로 전체 게시물을 넣어 관리