CURLStringFile::__construct

(PHP 8 >= 8.1.0)

CURLStringFile::__constructCURLStringFile オブジェクトを作る

説明

public CURLStringFile::__construct(string $data, string $postname, string $mime = "application/octet-stream")

CURLStringFile オブジェクトを作ります。 これは、CURLOPT_POSTFIELDS でファイルをアップロードするときに使います。

パラメータ

data

アップロードするデータ。

postname

アップロードするデータの中のファイルの名前。

mime

ファイルの MIME タイプ(デフォルトは application/octet-stream)

例1 CURLStringFile::__construct() の例

<?php
/* http://example.com/upload.php:
<?php
var_dump($_FILES);
var_dump(file_get_contents($_FILES['test_string']['tmp_name']));
?>
*/

// cURL ハンドルを作ります
$ch = curl_init('http://example.com/upload.php');

// CURLStringFile オブジェクトを作ります
$cstringfile = new CURLStringFile('test upload contents','test.txt','text/plain');

// POST データを設定します
$data = array('test_string' => $cstringfile);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

// このハンドルを実行します
curl_exec($ch);
?>

上の例の出力は以下となります。

array(1) {
  ["test_string"]=>
  array(5) {
    ["name"]=>
    string(8) "test.txt"
    ["type"]=>
    string(10) "text/plain"
    ["tmp_name"]=>
    string(14) "/tmp/phpTtaoCz"
    ["error"]=>
    int(0)
    ["size"]=>
    int(20)
  }
}
string(20) "test upload contents"

参考

add a note add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top