请注意,$postname 和 $mimetype 参数的顺序与 CURLFile::__construct 函数相反。(PHP 8 >= 8.1.0)
CURLStringFile::__construct — 创建一个 CURLStringFile 对象
$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"