Data (RFC 2397)
The data: (» RFC 2397) stream wrapper is available since PHP 5.2.0.
Пример #1 Print data:// contents
<?php
// prints "I love PHP"
echo file_get_contents('data://text/plain;base64,SSBsb3ZlIFBIUAo=');
?>
Пример #2 Fetch the media type
<?php
$fp = fopen('data://text/plain;base64,', 'r');
$meta = stream_get_meta_data($fp);
// prints "text/plain"
echo $meta['mediatype'];
?>
Attribute | Supported |
---|---|
Restricted by allow_url_fopen | No |
Restricted by allow_url_include | Yes |
Allows Reading | Yes |
Allows Writing | No |
Allows Appending | No |
Allows Simultaneous Reading and Writing | No |
Supports stat() | No |
Supports unlink() | No |
Supports rename() | No |
Supports mkdir() | No |
Supports rmdir() | No |
- Функция file://() - Доступ к локальной файловой системе
- Функция http://() - Доступ к URL-адресам по протоколу HTTP(s)
- Функция ftp://() - Доступ к URL-адресам по протоколу FTP(s)
- Функция php://() - Доступ к различным потокам ввода-вывода
- Функция zlib://() - Сжатые потоки
- Функция data://() - Схема Data (RFC 2397)
- Функция glob://() - Нахождение путей, соответствующих шаблону
- Функция phar://() - PHP архив
- Функция ssh2://() - Secure Shell 2
- Функция rar://() - RAR
- Функция ogg://() - Аудио потоки
- Функция expect://() - Потоки для взаимодействия с процессами
Коментарии
Now PHP supports data: protocol w/out "//" like data:text/plain, not data://text/plain,
I tried it.
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.
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
?>