php教程

data://

data://数据(RFC 2397)

说明

自 PHP 5.2.0 起 data:» RFC 2397)数据流封装器开始有效。

用法

  • 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

范例

Example #1 打印 data:// 的内容

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

Example #2 获取媒体类型

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

// 打印 "text/plain"
echo $meta['mediatype'];
?>

User Contributed Notes

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.

CopyRight © 2008-2022 verySource.Com All Rights reserved. 京ICP备17048824号-1 京公网安备:11010502034788