반응형
PHP로 curl을 사용하여 파일을 업로드하는 방법
cURL 또는 PHP에서 다른 것을 사용하여 파일을 업로드하는 방법을 알고 싶습니다. Google에서 여러 번 검색했지만 결과가 없습니다.
즉, 사용자가 양식에서 파일 업로드 버튼을보고 양식이 내 PHP 스크립트에 게시 된 다음 내 PHP 스크립트가 다른 스크립트 (예 : 다른 서버)에 다시 게시해야합니다.
파일을 받고 업로드하는 코드가 있습니다.
코드 :
echo"".$_FILES['userfile']."";
$uploaddir = './';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
if ( isset($_FILES["userfile"]) ) {
echo '<p><font color="#00FF00" size="7">Uploaded</font></p>';
if (move_uploaded_file
($_FILES["userfile"]["tmp_name"], $uploadfile))
echo $uploadfile;
else echo '<p><font color="#FF0000" size="7">Failed</font></p>';
}
코드가 파일을 수신자 파일로 보내고 싶습니다.
사용하다:
if (function_exists('curl_file_create')) { // php 5.5+
$cFile = curl_file_create($file_name_with_full_path);
} else { //
$cFile = '@' . realpath($file_name_with_full_path);
}
$post = array('extra_info' => '123456','file_contents'=> $cFile);
$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);
다음을 참조 할 수도 있습니다.
http://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/
PHP 5.5 이상에 대한 중요한 힌트 :
이제 https://wiki.php.net/rfc/curl-file-upload 를 사용해야 하지만이 더 이상 사용되지 않는 접근 방식을 계속 사용하려면 다음을 설정해야합니다.curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
참고 URL : https://stackoverflow.com/questions/15200632/how-to-upload-file-using-curl-with-php
반응형
'Nice programing' 카테고리의 다른 글
Java에서 두 문자열을 비교하고 알파벳순으로 다른 문자열보다 작은 문자열을 어떻게 정의 할 수 있습니까? (0) | 2020.11.20 |
---|---|
Snow Leopard에서 .profile과 .bash_profile의 차이점 (0) | 2020.11.20 |
Amazon CloudFront는 내 S3 웹 사이트 버킷의 index.html 규칙을 존중하지 않습니다. (0) | 2020.11.20 |
테스트 생성, 시도 생성, 생성 캐치 중 가장 바람직한 디자인은 무엇입니까? (0) | 2020.11.20 |
MySQL 테이블 기본 키를 일부 접두사로 자동 증가시키는 방법 (0) | 2020.11.20 |