downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

glob://> <zlib://
[edit] Last updated: Fri, 10 Feb 2012

view this page in

data://

data://データ (RFC 2397)

説明

data: (» RFC 2397) ストリームラッパーは、 PHP 5.2.0 以降で使用可能です。

オプション

  • data://text/plain;base64,

オプション

ラッパーの概要
属性 サポートの有無
allow_url_fopen で制約される No
allow_url_include で制約される Yes
読み込み許可 Yes
書き込み許可 No
追加許可 No
同時読み書き許可 No
stat() のサポート No
unlink() のサポート No
rename() のサポート No
mkdir() のサポート No
rmdir() のサポート No

例1 data:// の内容の表示

<?php
// "I love PHP" と表示します
echo file_get_contents('data://text/plain;base64,SSBsb3ZlIFBIUAo=');
?>

例2 media type の取得

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

// "text/plain" と表示します
echo $meta['mediatype'];
?>


glob://> <zlib://
[edit] Last updated: Fri, 10 Feb 2012
 
add a note add a note User Contributed Notes data://
senica at gmail dot com 03-Oct-2011 01:20
You can use data:// to evaluate php that is stored in mySQL

For example:

If you have code stored that looks like this:
$content = ' //Content from your database or variable
  <?php if($var == "something"): ?>
    print this html
  <?php endif; ?>
';

Trying to evaluate this can prove cumbersome

But you can do something like:

include "data://text/plain;base64,".base64_encode($content);

and it will parse it with no problems.
from dot php dot net at brainbox dot cz 18-Nov-2010 04:37
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
?>
admin deskbitz net 02-May-2010 12:54
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.
sandaimespaceman at gmail dot com 06-Sep-2008 06:30
Now PHP supports data: protocol w/out "//" like data:text/plain, not data://text/plain,

I tried it.
togos00 at gmail dot com 08-Apr-2008 12:56
Note that the official data URI scheme does not include a double slash after the colon - that you must include it when making calls to PHP is an artifact of the designers' misunderstanding of URL syntax.

To automatically convert proper data URIs to ones understood by PHP, you can use code such as the following:

<?php
function convertUriForPhp( $uri ) {
    if(
preg_match('/^data:(?!\\/\\/)(.*)$/',$uri,$bif) ) {
        return
'data://' . $bif[1];
    } else {
        return
$uri;
    }
}
?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites