PHP 5.5.16 is released

data://

data://Data (RFC 2397)

Descripción

La envoltura del flujo data: (» RFC 2397) está disponible desde PHP 5.2.0.

Uso

  • data://text/plain;base64,

Opciones

Resumen de la envolutra
Atributo Permitido
Restringido por allow_url_fopen No
Restringido por allow_url_include
Permite Lecturas
Permite Escrituras No
Permite Añadir contenido No
Permite Lectura y Escritura Simultanea No
Permite usar la función stat() No
Permite usar la función unlink() No
Permite usar la función rename() No
Permite usar la función mkdir() No
Permite usar la función rmdir() No

Ejemplos

Ejemplo #1 Mostrar contenidos de data://

<?php
// muestra "I love PHP"
echo file_get_contents('data://text/plain;base64,SSBsb3ZlIFBIUAo=');
?>

Ejemplo #2 Obtener el Tipo de Medio

<?php
$fp   
fopen('data://text/plain;base64,''r');
$meta stream_get_meta_data($fp);

// muestra "text/plain"
echo $meta['mediatype'];
?>
add a note add a note

User Contributed Notes 3 notes

up
5
admin deskbitz net
4 years ago
If you want to create a gd-image directly out of a sql-database-field you might want to use:

<?php
$jpegimage
= imagecreatefromjpeg("data://image/jpeg;base64," . base64_encode($sql_result_array['imagedata']));
?>

this goes also for gif, png, etc using the correct "imagecreatefrom$$$"-function and mime-type.
up
5
sandaimespaceman at gmail dot com
5 years ago
Now PHP supports data: protocol w/out "//" like data:text/plain, not data://text/plain,

I tried it.
up
2
from dot php dot net at brainbox dot cz
3 years ago
When passing plain string without base64 encoding, do not forget to pass the string through URLENCODE(), because PHP automatically urldecodes all entities inside passed string (and therefore all + get lost, all % entities will be converted to the corresponding characters).

In this case, PHP is strictly compilant with the RFC 2397. Section 3 states that passes data should be either in base64 encoding or urlencoded.

VALID USAGE:
<?php
$fp
= fopen('data:text/plain,'.urlencode($data), 'rb'); // urlencoded data
$fp = fopen('data:text/plain;base64,'.base64_encode($data), 'rb'); // base64 encoded data
?>

Demonstration of invalid usage:
<?php
$data
= 'Günther says: 1+1 is 2, 10%40 is 20.';

$fp = fopen('data:text/plain,'.$data, 'rb'); // INVALID, never do this
echo stream_get_contents($fp);
// Günther says: 1 1 is 2, 10@ is 20. // ERROR

$fp = fopen('data:text/plain,'.urlencode($data), 'rb'); // urlencoded data
echo stream_get_contents($fp);
// Günther says: 1+1 is 2, 10%40 is 20. // OK

// Valid option 1: base64 encoded data
$fp = fopen('data:text/plain;base64,'.base64_encode($data), 'rb'); // base64 encoded data
echo stream_get_contents($fp);
// Günther says: 1+1 is 2, 10%40 is 20. // OK
?>
To Top