data://

data:// β€” Π‘Ρ…Π΅ΠΌΠ° Data (RFC 2397)

ОписаниС

data: (» RFC 2397) β€” ΠΎΠ±Ρ‘Ρ€Ρ‚ΠΊΠ° ΠΏΠΎΡ‚ΠΎΠΊΠΎΠ².

ИспользованиС

  • data://text/plain;base64,

ΠžΠΏΡ†ΠΈΠΈ

Основная информация
Атрибут ΠŸΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ°
ΠžΠ³Ρ€Π°Π½ΠΈΡ‡Π΅Π½ΠΈΠ΅ ΠΏΠΎ allow_url_fopen Π”Π°
ΠžΠ³Ρ€Π°Π½ΠΈΡ‡Π΅Π½ΠΈΠ΅ ΠΏΠΎ allow_url_include Π”Π°
Π§Ρ‚Π΅Π½ΠΈΠ΅ Π”Π°
Π—Π°ΠΏΠΈΡΡŒ НСт
Π”ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ НСт
Π§Ρ‚Π΅Π½ΠΈΠ΅ ΠΈ запись ΠΎΠ΄Π½ΠΎΠ²Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎ НСт
ΠŸΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° stat() НСт
ΠŸΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° unlink() НСт
ΠŸΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° rename() НСт
ΠŸΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° mkdir() НСт
ΠŸΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° rmdir() НСт

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 Π’Ρ‹Π²ΠΎΠ΄ содСрТимого data://

<?php

// Π’Ρ‹Π²ΠΎΠ΄ΠΈΡ‚: I love PHP
echo file_get_contents('data://text/plain;base64,SSBsb3ZlIFBIUAo=');

?>

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #2 ΠŸΠΎΠ»ΡƒΡ‡Π΅Π½ΠΈΠ΅ Ρ‚ΠΈΠΏΠ° ΠΏΠΎΡ‚ΠΎΠΊΠ°

<?php

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

// Π’Ρ‹Π²ΠΎΠ΄ΠΈΡ‚: text/plain
echo $meta['mediatype'];

?>
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 3 notes

up
28
from dot php dot net at brainbox dot cz ΒΆ
15 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
?>
up
13
admin deskbitz net ΒΆ
16 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 ΒΆ
17 years ago
Now PHP supports data: protocol w/out "//" like data:text/plain, not data://text/plain,

I tried it.