PHPのお勉強!

PHP TOP

サポートするプロトコル/ラッパー

PHP には多くの組み込みラッパーが用意されています。さまざまな URL 風のプロトコルによって、 fopen()copy()file_exists() そして filesize() といったファイルシステム関数で使用することができます。 これらのラッパーだけでなく、 stream_wrapper_register() 関数でラッパーを自作することもできます。

注意: ラッパーを指定する際の URL 構文としてサポートしているのは scheme://... 形式のみです。 scheme:/scheme: といった形式には対応していません。

目次

  • file:// — ローカルファイルシステムへのアクセス
  • http:// — HTTP(s) URL へのアクセス
  • ftp:// — FTP(s) URL へのアクセス
  • php:// — さまざまな入出力ストリームへのアクセス
  • zlib:// — 圧縮ストリーム
  • data:// — データ (RFC 2397)
  • glob:// — パターンにマッチするパス名の検索
  • phar:// — PHP アーカイブ
  • ssh2:// — Secure Shell 2
  • rar:// — RAR
  • ogg:// — オーディオストリーム
  • expect:// — 対話的プロセスストリーム
add a note

User Contributed Notes 31 notes

up
24
fabacrans__ at __nospamhotmail__ dot __com
11 years ago
You can use "php://input" to accept and parse "PUT", "DELETE", etc. requests.

<?php
// Example to parse "PUT" requests
parse_str(file_get_contents('php://input'), $_PUT);

// The result
print_r($_PUT);
?>

(very useful for Restful API)
up
17
ben dot johansen at gmail dot com
18 years ago
Example of how to use the php://input to get raw post data

//read the raw data in
$roughHTTPPOST = file_get_contents("php://input");
//parse it into vars
parse_str($roughHTTPPOST);

if you do readfile("php://input") you will get the length of the post data
up
13
aidan at php dot net
20 years ago
The contants:

* STDIN
* STDOUT
* STDERR

Were introduced in PHP 4.3.0 and are synomous with the fopen('php://stdx') result resource.
up
18
sander at medicore dot nl
17 years ago
to create a raw tcp listener system i use the following:

xinetd daemon with config like:
service test
{
disable = no
type = UNLISTED
socket_type = stream
protocol = tcp
bind = 127.0.0.1
port = 12345
wait = no
user = apache
group = apache
instances = 10
server = /usr/local/bin/php
server_args = -n [your php file here]
only_from = 127.0.0.1 #gotta love the security#
log_type = FILE /var/log/phperrors.log
log_on_success += DURATION
}

now use fgets(STDIN) to read the input. Creates connections pretty quick, works like a charm.Writing can be done using the STDOUT, or just echo. Be aware that you're completely bypassing the webserver and thus certain variables will not be available.
up
13
ben dot johansen at gmail dot com
18 years ago
In trying to do AJAX with PHP and Javascript, I came upon an issue where the POST argument from the following javascript could not be read in via PHP 5 using the $_REQUEST or $_POST. I finally figured out how to read in the raw data using the php://input directive.

Javascript code:
=============
//create request instance
xhttp = new XMLHttpRequest();
// set the event handler
xhttp.onreadystatechange = serviceReturn;
// prep the call, http method=POST, true=asynchronous call
var Args = 'number='+NbrValue;
xhttp.open("POST", "http://<?php echo $_SERVER['SERVER_NAME'] ?>/webservices/ws_service.php", true);
// send the call with args
xhttp.send(Args);

PHP Code:
//read the raw data in
$roughHTTPPOST = file_get_contents("php://input");
//parse it into vars
parse_str($roughHTTPPOST);
up
11
php at rapsys dot eu
12 years ago
Here is a snippet to read compressed raw post data without enabling global variables.

I needed it to read xml posted data submitted by ocs agent. The data was sent as Content-Type: application/x-compressed (zlib compressed data).

It seems related to an old bug which still seems broken :
https://bugs.php.net/bug.php?id=49411

The important part is the default window set to 15 instead of -15.

Code snippet
<?php
$data
= '';
$fh = fopen('php://input', 'rb');
stream_filter_append($fh, 'zlib.inflate', STREAM_FILTER_READ, array('window'=>15));
while(!
feof($fh)) {
$data .= fread($fh, 8192);
}
?>
up
10
sebastian dot krebs at kingcrunch dot de
13 years ago
The stream php://temp/maxmemory:$limit stores the data in memory unless the limit is reached. Then it will write the whole content the a temporary file and frees the memory. I didnt found a way to get at least some of the data back to memory.
up
12
Hayley Watson
6 years ago
Even though their names will be the same, you can have more than one //memory or //temp stream open concurrently; each time you fopen() such a stream, a NEW stream will be opened independently of the others.

This is hinted at by the fact you don't add any unique identifier to the path when creating such streams, but isn't said explicitly.

<?php

$hello
= fopen('php://memory', 'r+'); // $hello, $php, $world are all different streams.
$php = fopen('php://memory', 'r+');
$world = fopen('php://memory', 'r+'); // They're not the same stream opened three times.

fputs($hello, "Hello ");
fputs($php, "PHP ");
rewind($php);
fputs($world, "World!");
rewind($hello);
rewind($world);

echo
'[', stream_get_contents($hello), '][', stream_get_contents($php), '][', stream_get_contents($world), ']';
// If they were the same stream the output would be "[World!][World!][World!]".
?>
up
12
gjaman at gmail dot com
16 years ago
You can decompress (gzip) a input stream by combining wrappers:

eg: $x = file_get_contents("compress.zlib://php://input");

I used this method to decompress a gzip stream that was pushed to my webserver
up
9
heitorsiller at uol dot com dot br
18 years ago
For reading a XML stream, this will work just fine:
<?php

$arq
= file_get_contents('php://input');

?>

Then you can parse the XML like this:

<?php

$xml
= xml_parser_create();

xml_parse_into_struct($xml, $arq, $vs);

xml_parser_free($xml);

$data = "";

foreach(
$vs as $v){

if(
$v['level'] == 3 && $v['type'] == 'complete')
$data .= "\n".$v['tag']." -> ".$v['value'];
}

echo
$data;

?>

PS.: This is particularly useful for receiving mobile originated (MO) SMS messages from cellular phone companies.
up
8
nargy at yahoo dot com
20 years ago
When opening php://output in append mode you get an error, the way to do it:
$fp=fopen("php://output","w");
fwrite($fp,"Hello, world !<BR>\n");
fclose($fp);
up
6
vibhavsinha91 at gmail dot com
10 years ago
While writing to error stream, error_log() function comes as a shorthand to writing to php://stderr . This function also allows writing to web server log when running through a web server such as apache.
up
4
Anonymous
6 years ago
Be forewarned:

the file:// protocol used in file_get_contents is used as the default for "any unrecognized protocol." Thus:

aldfjadlfadfladfl://whatever

will deliver the same as

file://whatever
up
7
Anonymous
7 years ago
If you want to filter incoming data through php://input use this:

file_get_contents("php://filter/read=string.strip_tags/resource=php://input");

I couldn't find any documentation to explain how to do this. All the examples I came across suggested that a full and actual URL had to be used (which didn't work for me).

This seems to work though.
up
6
Justin Megawarne
11 years ago
If my understanding of the implementing code is correct, every time you open a php://memory stream, you get new storage allocated. That is to say, php://memory isn't a shared bank of memory.
up
7
sam at bigwig dot net
21 years ago
[ Editor's Note: There is a way to know. All response headers (from both the final responding server and intermediate redirecters) can be found in $http_response_header or stream_get_meta_data() as described above. ]

If you open an HTTP url and the server issues a Location style redirect, the redirected contents will be read but you can't find out that this has happened.

So if you then parse the returned html and try and rationalise relative URLs you could get it wrong.