Note that the $postname and $mimetype parameters are in the reverse order to the CURLFile::__construct function.PHP - Manual: CURLStringFile::__construct
2025-10-24
(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"
官方地址:https://www.php.net/manual/en/curlstringfile.construct.php