2010-04-05 4 views
7

(html 형식) enctype = "multipart/form-data", 입력 유형 = "file"등으로 클라이언트에서 서버로 파일을 업로드 할 수 있습니다.서버에서 서버로의 http 전송 파일

파일을 서버에 이미 가지고 다른 서버로 전송하는 방법이 있습니까?

힌트를 보내 주셔서 감사합니다.

// 와우! 이것은 내가 본 가장 빠른 질문 응답 페이지입니다!

답변

10

브라우저가 파일을 서버에 업로드하면 파일의 내용이 들어있는 HTTP POST 요청을 보냅니다.

당신은 그것을 복제해야합니다. 솔루션 curl와 함께 작동하도록 아마도 (적어도, 가장 많이 사용하거나,) 간단한 PHP와


. 당신은 당신이 curl_setopt으로 설정할 수있는 옵션 목록을 살펴 경우

, 당신은 볼이 하나 CURLOPT_POSTFIELDS (인용) : HTTP를에 게시

전체 데이터를 "POST" 작업.
파일을 게시하려면 앞에 @와 파일 이름을 붙이고 전체 경로을 사용하십시오.
이 어느 이 같은 를 urlencoded 문자열 'para1 = VAL1 para2 & &을 val2 = ...'값 또는 키 필드로서 데이터 필드 명을 갖는 배열로 로 전달 될 수있다.
값이 인 배열 인 경우 Content-Type 헤더는 이 multipart/form-data로 설정됩니다.,

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/your-destination-script.php"); 
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
     'file' => '@/..../file.jpg', 
     // you'll have to change the name, here, I suppose 
     // some other fields ? 
)); 
$result = curl_exec($ch); 
curl_close($ch); 

을 기본적으로 당신을 :

    적어도 시작하는 데 도움이, 또는 -


테스트하지,하지만이 같은 트릭을해야한다고 가정
  • 은 컬을 사용하고 있습니다
  • 도착 URL을 설정해야합니다
  • 결과를 반환하는 curl_exec을 원하고, 출력하지 그것을
  • POST을 사용하고 있지 GET
  • 것은 파일을 포함하여 일부 데이터를 게시하는 - 파일의 경로 전에 @주의를.
  • +0

    나는 이것이 작동 할 것이라고 생각하고 이것을 시도 할 것이다! 감사합니다 :-) (안녕하세요, 저는 그 때와 같은 성을 가지고있어서 대답은 맞습니다 ^^) – qualle

    +0

    오신 것을 환영합니다 :-) 재미있게 보내십시오! * –

    +0

    +1, 나는 PHP로'@ '기호를 사용하는 것만 큼 쉽지 않았다는 것을 깨닫지 못했다. :-) –

    0

    Ex. 서버 A에 mypicture.gif라는 파일이 있고이를 서버 B로 보내려면 CURL을 사용할 수 있습니다.

    1. 서버 A는 내용을 문자열로 읽습니다.
    2. 포스트

    http://php.net/manual/en/book.curl.php

    일부 샘플 PHP 코드를 참조 서버 B는라는 파일 mypictyre-copy.gif 같은 문자열 저장을 가져

  • 서버 B에 곱슬 문자열 :

    <?php 
        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_HEADER, 0); 
        curl_setopt($ch, CURLOPT_VERBOSE, 0); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)"); 
        curl_setopt($ch, CURLOPT_URL, _VIRUS_SCAN_URL); 
        curl_setopt($ch, CURLOPT_POST, true); 
        // same as <input type="file" name="file_box"> 
        $post = array(
         "file_box"=>"@/path/to/myfile.jpg", 
        ); 
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
        $response = curl_exec($ch); 
    ?> 
    
  • 0

    서버가 모두 제어되는 경우 FTP가 HTTP보다 더 나은 선택 일 수 있습니다.

    +0

    불행히도 – qualle

    +0

    이 아닙니다. 방화벽은 방화벽과 NAT 게이트웨이를 통과하는 것이 번거롭기 때문에 좋지 않습니다. HTTP를 사용하거나 HTTPS를 사용하는 것이 좋습니다. 고급 솔루션은 webdav도 고려하지만 결코 FTP를 사용하지 마십시오! – Raven

    +0

    두 서버가 모두 Asker의 제어를받는다면 (답변에서 말했듯이) 방화벽에 문제가있는 것을 볼 수 없습니까? –

    3

    당신도 똑같이 할 수 있습니다. 이번에는 파일을받은 서버가 클라이언트이고 두 번째 서버가 서버입니다. 첫 번째 서버에 파일을 보내

    <form> 
         <input type="text" name="var1" /> 
         <input type="text" name="var2" /> 
         <input type="file" name="inputname" /> 
         <input type="submit" /> 
        </form> 
    

    그리고 스크립트로 : 두 번째 서버에있는 웹 페이지의 경우

    : 이를 사용해보십시오

    <?php 
    function PostToHost($host, $port, $path, $postdata, $filedata) { 
        $data = ""; 
        $boundary = "---------------------".substr(md5(rand(0,32000)),0,10); 
        $fp = fsockopen($host, $port); 
    
        fputs($fp, "POST $path HTTP/1.0\n"); 
        fputs($fp, "Host: $host\n"); 
        fputs($fp, "Content-type: multipart/form-data; boundary=".$boundary."\n"); 
    
        // Ab dieser Stelle sammeln wir erstmal alle Daten in einem String 
        // Sammeln der POST Daten 
        foreach($postdata as $key => $val){ 
         $data .= "--$boundary\n"; 
         $data .= "Content-Disposition: form-data; name=\"".$key."\"\n\n".$val."\n"; 
        } 
        $data .= "--$boundary\n"; 
    
        // Sammeln der FILE Daten 
        $data .= "Content-Disposition: form-data; name=\"{$filedata[0]}\"; filename=\"{$filedata[1]}\"\n"; 
        $data .= "Content-Type: image/jpeg\n"; 
        $data .= "Content-Transfer-Encoding: binary\n\n"; 
        $data .= $filedata[2]."\n"; 
        $data .= "--$boundary--\n"; 
    
        // Senden aller Informationen 
        fputs($fp, "Content-length: ".strlen($data)."\n\n"); 
        fputs($fp, $data); 
    
        // Auslesen der Antwort 
        while(!feof($fp)) { 
         $res .= fread($fp, 1); 
        } 
        fclose($fp); 
    
        return $res; 
    } 
    
    $postdata = array('var1'=>'test', 'var2'=>'test'); 
    $data = file_get_contents('Signatur.jpg'); 
    $filedata = array('inputname', 'filename.jpg', $data); 
    
    echo PostToHost ("localhost", 80, "/test3.php", $postdata, $filedata); 
    ?> 
    

    두 스크립트는 여기에서 가지고있다 : http://www.coder-wiki.de/HowTos/PHP-POST-Request-Datei