CURLStringFile::__construct
(PHP 8 >= 8.1.0)
CURLStringFile::__construct — 建立 CURLStringFile 對像
說明
public CURLStringFile::__construct(string
$data, string $postname, string $mime = "application/octet-stream")
建立 CURLStringFile 對象,使用 CURLOPT_POSTFIELDS 選項上傳檔案。
參數
-
data -
待上傳的內容。
-
postname -
上傳數據中的檔名稱。
-
mime -
檔案的 MIME 型別(預設為
application/octet-stream)。
返回值
返回 CURLStringFile 對象。
範例
示例 #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"