# PHP data://

PHPServer Side ProgrammingProgramming

## Introduction

The data URI scheme has been defined in RFC 2397, published in 1998. It provides a mechanism to include in-line data in web page as if it is an external resource. PHP provides data:// wrapper for data URI representation. The data URI is represented as per following syntax

## data:// syntax

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

parameters

media type − Default is text/plain

optional base64 extension base64, separated from the preceding part by a semicolon, ndicating that the data content is binary data, encoded using the Base64 scheme for binary-to-text encoding.

The data, separated from the preceding part by a comma (,). The data is a sequence of zero or more octets represented as characters.

## Examples

Following example encodes a string to base64 format and then uses it as data in data:// URI

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

We can also use file_get_contents() function to fetch data from a file to convert in bas64 format

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

Following example uses text/html as media type in data:// wrapper

<?php
$string=file_get_contents("test.html");$b64=base64_encode($string); echo file_get_contents('data://text/html;base64,'.$b64);
?>
Published on 22-Sep-2020 14:07:59