(PHP 5 >= 5.5.0, PHP 7)
CURLFile::__construct -- curl_file_create — CURLFile オブジェクトを作る
オブジェクト指向型
$filename
[, string $mimetype
[, string $postname
]] )手続き型
CURLFile オブジェクトを作ります。これは、CURLOPT_POSTFIELDS
でファイルをアップロードするときに使います。
filenameアップロードするファイルへのパス。
mimetypeファイルの Mimetype。
postnameアップロードデータの中で使うファイルの名前。
CURLFile オブジェクトを返します。
例1 CURLFile::__construct() の例
オブジェクト指向型
<?php
/* http://example.com/upload.php:
<?php var_dump($_FILES); ?>
*/
// cURL ハンドルを作ります
$ch = curl_init('http://example.com/upload.php');
// CURLFile オブジェクトを作ります
$cfile = new CURLFile('cats.jpg','image/jpeg','test_name');
// POST データを設定します
$data = array('test_file' => $cfile);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// このハンドルを実行します
curl_exec($ch);
?>
手続き型
<?php
/* http://example.com/upload.php:
<?php var_dump($_FILES); ?>
*/
// cURL ハンドルを作ります
$ch = curl_init('http://example.com/upload.php');
// CURLFile オブジェクトを作ります
$cfile = curl_file_create('cats.jpg','image/jpeg','test_name');
// POST データを設定します
$data = array('test_file' => $cfile);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// このハンドルを実行します
curl_exec($ch);
?>
上の例の出力は以下となります。
array(1) {
["test_file"]=>
array(5) {
["name"]=>
string(9) "test_name"
["type"]=>
string(10) "image/jpeg"
["tmp_name"]=>
string(14) "/tmp/phpPC9Kbx"
["error"]=>
int(0)
["size"]=>
int(46334)
}
}