函式名:data://()
適用版本:PHP 5 >= 5.2.0, PHP 7
用法:data://() 是一個資料流封裝器函式,用於透過訪問記憶體中的資料來建立資料流。該函式允許您直接在指令碼中運算元據,而無需實際的物理檔案。
語法:data://text/plain;base64,{data}
引數:
- data:要在資料流中包含的資料,可以是任何字串。
返回值:data://() 函式返回一個資料流資源。
示例:
<?php
$data = 'This is the content of the data stream.';
// 建立資料流
$stream = fopen('data://text/plain;base64,' . base64_encode($data), 'r');
// 讀取資料流
$content = stream_get_contents($stream);
echo $content; // 輸出:This is the content of the data stream.
// 關閉資料流
fclose($stream);
?>
上述示例中,我們透過使用 data://() 函式建立了一個資料流,並指定了資料的型別為 text/plain,並將資料使用 base64 編碼。然後使用 fopen() 函式開啟資料流,並使用 stream_get_contents() 函式讀取資料流中的內容。最後,我們輸出了資料流中的內容。
請注意,data://() 函式還可以用於將資料流傳遞給其他 PHP 函式,以便進行進一步的處理。