CURLStringFile::__construct

(PHP 8 >= 8.1.0)

CURLStringFile::__construct創(chuàng)建 CURLStringFile 對象

說明

public CURLStringFile::__construct(string $data, string $postname, string $mime = "application/octet-stream")

創(chuàng)建 CURLStringFile 對象,使用 CURLOPT_POSTFIELDS 選項上傳文件。

參數(shù)

data

待上傳的內容。

postname

上傳數(shù)據(jù)中的文件名稱。

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']));
?>
*/

// 創(chuàng)建一個 cURL 句柄
$ch curl_init('http://example.com/upload.php');

// 創(chuàng)建一個 CURLStringFile 對象
$cstringfile = new CURLStringFile('test upload contents','test.txt','text/plain');

// 指定 POST 內容
$data = array('test_string' => $cstringfile);
curl_setopt($chCURLOPT_POST1);
curl_setopt($chCURLOPT_POSTFIELDS$data);

// 執(zhí)行句柄
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"

參見