IT/php

php multipart 사용법 stream_context_create

조원태 2015. 11. 23. 17:13
반응형

파일 명 : addheader.php

<?php 

$eol = "\r\n";
$data = '';

$mime_boundary=md5(time());

$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="somedata"' . $eol . $eol;
$data .= "Some Data" . $eol;
$data .= '--' . $mime_boundary . $eol;
$data .= 'Content-Disposition: form-data; name="somefile"; filename="filename.ext"' . $eol;
$data .= 'Content-Type: text/plain' . $eol;
$data .= 'Content-Transfer-Encoding: base64' . $eol . $eol;
$data .= chunk_split(base64_encode("Some file content")) . $eol;
$data .= "--" . $mime_boundary . "--" . $eol . $eol; // finish with two eol's!!

$params = array('http' => array(
                  'method' => 'POST',
                  'header' => 'Content-Type: multipart/form-data; boundary=' . $mime_boundary . $eol,
                  'content' => $data
               ));

$ctx = stream_context_create($params);
$response = @file_get_contents($destination, FILE_TEXT, $ctx);

 

echo $response; 

?> 

 

 

stream_context_create

stream_context_create - 스트림 문맥을 작성합니다

 

헤더 정보에 넣을 context를 만든다.

​file_get_contents($destination, FILE_TEXT, $ctx);

 

destination(url주소)에 $ctx 정보(헤더)를 넣는다.

 

이렇게 되면  

destination에 $ctx 정보가 포함되어 html문서로 만들어짐



 

반응형