CascadiaPHP 2024


data://Daten (RFC 2397)


Der Stream-Wrapper data: (» RFC 2397).


  • data://text/plain;base64,


Zusammenfassung des Wrappers
Eigenschaft wird unterstützt
Eingeschränkt durch allow_url_fopen Ja
Eingeschränkt durch allow_url_include Ja
Erlaubt Lesen Ja
Erlaubt Schreiben Nein
Erlaubt Anfügen Nein
Erlaubt gleichzeitiges Lesen und Schreiben Nein
Unterstützt stat() Nein
Unterstützt unlink() Nein
Unterstützt rename() Nein
Unterstützt mkdir() Nein
Unterstützt rmdir() Nein


Beispiel #1 Ausgabe des Inhalts von data://

// gibt "Ich liebe PHP" aus
echo file_get_contents('data://text/plain;base64,SSBsb3ZlIFBIUAo=');

Beispiel #2 Abrufen des Medientyps

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

// gibt "text/plain" aus
echo $meta['mediatype'];
add a note

User Contributed Notes 3 notes

from dot php dot net at brainbox dot cz
13 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.

= 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:
= '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
admin deskbitz net
14 years ago
If you want to create a gd-image directly out of a sql-database-field you might want to use:

= 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.
sandaimespaceman at gmail dot com
15 years ago
Now PHP supports data: protocol w/out "//" like data:text/plain, not data://text/plain,

I tried it.
To Top