請注意,$postname 和 $mimetype 參數的順序與 CURLFile::__construct 函式相反。
(PHP 8 >= 8.1.0)
CURLStringFile::__construct — 建立 CURLStringFile 物件
建立一個 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"