(html 형식) enctype = "multipart/form-data", 입력 유형 = "file"등으로 클라이언트에서 서버로 파일을 업로드 할 수 있습니다.서버에서 서버로의 http 전송 파일
파일을 서버에 이미 가지고 다른 서버로 전송하는 방법이 있습니까?
힌트를 보내 주셔서 감사합니다.
// 와우! 이것은 내가 본 가장 빠른 질문 응답 페이지입니다!
(html 형식) enctype = "multipart/form-data", 입력 유형 = "file"등으로 클라이언트에서 서버로 파일을 업로드 할 수 있습니다.서버에서 서버로의 http 전송 파일
파일을 서버에 이미 가지고 다른 서버로 전송하는 방법이 있습니까?
힌트를 보내 주셔서 감사합니다.
// 와우! 이것은 내가 본 가장 빠른 질문 응답 페이지입니다!
브라우저가 파일을 서버에 업로드하면 파일의 내용이 들어있는 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);
을 기본적으로 당신을 :
적어도 시작하는 데 도움이, 또는 -
curl_exec
을 원하고, 출력하지 그것을POST
을 사용하고 있지 GET
@
주의를.Ex. 서버 A에 mypicture.gif라는 파일이 있고이를 서버 B로 보내려면 CURL을 사용할 수 있습니다.
이 http://php.net/manual/en/book.curl.php
일부 샘플 PHP 코드를 참조 서버 B는라는 파일 mypictyre-copy.gif 같은 문자열 저장을 가져
<?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);
?>
당신도 똑같이 할 수 있습니다. 이번에는 파일을받은 서버가 클라이언트이고 두 번째 서버가 서버입니다. 첫 번째 서버에 파일을 보내
<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
나는 이것이 작동 할 것이라고 생각하고 이것을 시도 할 것이다! 감사합니다 :-) (안녕하세요, 저는 그 때와 같은 성을 가지고있어서 대답은 맞습니다 ^^) – qualle
오신 것을 환영합니다 :-) 재미있게 보내십시오! * –
+1, 나는 PHP로'@ '기호를 사용하는 것만 큼 쉽지 않았다는 것을 깨닫지 못했다. :-) –