PHP data://


簡介

資料 URI 協議於 1998 年釋出的 RFC 2397 中定義。它提供了一種機制,以將行內資料包含在網頁中,彷彿它是一個外部資源。PHP 為資料 URI 表示形式提供 data:// 包裝器。資料 URI 按照以下語法表示:

data:// 語法

data:[media type][;base64],data

引數

媒體型別- 預設值為 text/plain

可選base64擴充套件 base64,用分號與前一部分分開,表示資料內容是二進位制資料,使用 Base64 編碼方案從二進位制編碼為文字。

資料,用逗號(,)與前一部分分開。資料是一個由字元表示的 0 個或多個八位元組序列。

範例

以下範例將一個字串編碼為 base64 格式,然後將其用作 data:// URI 中的資料:

<?php
$string="TutorialsPoint India (p) Ltd";
$b64=base64_encode($string);
echo file_get_contents('data://text/plain;base64,'. $b64);
?>

我們也可以使用file_get_contents() 函式從檔案提取資料並轉換為 bas64 格式

<?php
$string=file_get_contents("test.txt");
$b64=base64_encode($string);
echo file_get_contents('data://text/plain;base64,'. $b64);
?>

以下範例在 data:// 包裝器中使用 text/html 作為媒體型別:

<?php
$string=file_get_contents("test.html");
$b64=base64_encode($string);
echo file_get_contents('data://text/html;base64,'. $b64);
?>

更新時間: 2020 年 09 月 22 日

182 次瀏覽

開啟你的 職業 生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.