라이브러리

[PHP] CURLStringFile::__construct - CURLStringFile 객체 생성




CURLStringFile::__construct


PHP의 CURLStringFile 클래스는 HTTP 요청의 파일 첨부를 지원하는 클래스입니다. 이 클래스는 PHP 5.5.0 버전부터 사용할 수 있습니다. CURLStringFile 클래스의 `__construct` 메서드는 파일 첨부를 위한 객체를 생성합니다.

# 생성자


`__construct` 메서드는 다음과 같은 매개 변수를 받습니다.

* `name`: 첨부할 파일의 이름입니다.
* `filename`: 첨부할 파일의 실제 이름입니다.
* `filecontents`: 첨부할 파일의 내용입니다.

# 예제


다음 예제는 CURLStringFile 클래스의 `__construct` 메서드를 사용하여 파일 첨부를 하는 방법을 보여줍니다.

#hostingforum.kr
php

// 첨부할 파일의 이름

$name = 'example.txt';



// 첨부할 파일의 실제 이름

$filename = 'example.txt';



// 첨부할 파일의 내용

$filecontents = 'Hello, World!';



// CURLStringFile 객체 생성

$curlFile = new CURLFile($filename, 'text/plain', $name);



// HTTP 요청에 첨부할 파일을 추가합니다.

$headers = array(

    'Content-Type: multipart/form-data',

    'Content-Disposition: form-data; name="file"; filename="' . $name . '"'

);



// HTTP 요청을 보냅니다.

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://example.com/upload');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => $curlFile));

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);

curl_close($ch);



// 첨부한 파일의 내용을 출력합니다.

echo $response;



# 참고


CURLStringFile 클래스의 `__construct` 메서드는 PHP 5.5.0 버전부터 사용할 수 있습니다. 이전 버전의 PHP에서는 이 클래스를 사용할 수 없습니다. 또한, 이 클래스는 HTTP 요청의 파일 첨부를 지원하기 때문에, `Content-Type` 헤더의 `multipart/form-data` 값을 지정해야 합니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 8,985건 / 567 페이지

검색

게시물 검색