2013-03-13

PHP 에서 curl 이용하여 파일 업로드 시

http://joshhighland.com/blog/2010/11/27/using-curl-and-php-to-upload-files-through-a-form-post/

PHP에서 cURL을 이용하여 파일을 업로드 할때

"@" 을 파일 경로 앞에 붙어 전송하면, 서버쪽에서는 _FILES 전역변수에 정보가 들어가 게 된다.


$file_name_with_full_path = realpath('./test.jpg');
$post = array('userfile'=>'@'.$file_name_with_full_path);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
curl_close ($ch);

//execute the API Call
$returned_data = curl_exec($curl_handle);


문제는 이런식으로 보내게 되면, 서버측 _FILES['type'] 이 "application/octet-stream" 로 표시되어, 파일 종류 검사에서 실패하게 된다.

이를 해결하는 하나의 방법은

$postFields['file'] = “@PATHTOFILE;type=CONTENTTYPEHERE”;

$post_param = array('userfile' => '@'.$file_name_with_full_path.';type='.$_FILES['userfile']['type']); 처럼 Content type을 파일경로 끝에 ';'와 함께 붙여서 전송 하면 된다.

No comments:

Post a Comment