Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Appendices : List of Supported Protocols/Wrappers

Appendix O. List of Supported Protocols/Wrappers

The following is a list of the various URL style protocols that PHP has built-in for use with the filesystem functions such as fopen() and copy(). In addition to these wrappers, as of PHP 4.3.0, you can write your own wrappers using PHP script and stream_wrapper_register().

Filesystem

All versions of PHP. Explicitly using file:// since PHP 5.0.0.

  • /path/to/file.ext
  • relative/path/to/file.ext
  • fileInCwd.ext
  • C:/path/to/winfile.ext
  • C:\path\to\winfile.ext
  • \\smbserver\share\path\to\winfile.ext
  • file:///path/to/file.ext

Filesystem is the default wrapper used with PHP and represents the local filesystem. When a relative path is specified (a path which does not begin with /, \, \\, or a Windows drive letter) the path provided will be applied against the current working directory. In many cases this is the directory in which the script resides unless it has been changed. Using the CLI sapi, this defaults to the directory from which the script was called.

With some functions, such as fopen() and file_get_contents(), include_path may be optionally searched for relative paths as well.

Table O.1. Wrapper Summary

Attribute Supported
Restricted by allow_url_fopen No
Allows Reading Yes
Allows Writing Yes
Allows Appending Yes
Allows Simultaneous Reading and Writing Yes
Supports stat() Yes
Supports unlink() Yes
Supports rename() Yes
Supports mkdir() Yes
Supports rmdir() Yes


Code Examples / Notes » wrappers

sam

[ 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.


nargy

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 !
\n");
fclose($fp);


sander

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.


aidan

The contants:
* STDIN
* STDOUT
* STDERR
Were introduced in PHP 4.3.0 and are synomous with the fopen('php://stdx') result resource.


nyvsld

php://stdin supports fseek() and fstat() function call,
while php://input doesn't.


opedroso

php://input allows you to read raw POST data. It is a less memory intensive alternative to $HTTP_RAW_POST_DATA and does not need any special php.ini directives.
Example use:
$httprawpostdata = file_get_contents("php://input");
When reading a base64 encoded stream using php://input, be aware that you do not need to decode it, it will automatically be done for you.


jerry

Not only are STDIN, STDOUT, and STDERR only allowed for CLI programs, but they are not allowed for programs that are read from STDIN. That can confuse you if you try to type in a simple test program.

ben dot johansen

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);


chris

If you're looking for a unix based smb wrapper there isn't one built in,  but I've had luck with http://www.zevils.com/cgi-bin/viewcvs.cgi/libsmbclient-php/ (tarball link at the end).

lupti

I find using file_get_contents with php://input is very handy and efficient. Here is the code:
$request = "";
$request = file_get_contents("php://input");
I don't need to declare the URL filr string as "r". It automatically handles open the file with read.
I can then use this $request string to your XMLparser as data.


heitorsiller

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.


ben dot johansen

followup:
I found that if I added this line to the AJAX call, the values would show up in the $_POST
xhttp.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');


drewish

Be aware that contrary to the way this makes it sound, under Apache, php://output and php://stdout don't point to the same place.
<?php
$fo = fopen('php://output', 'w');
$fs = fopen('php://stdout', 'w');
fputs($fo, "You can see this with the CLI and Apache.\n");
fputs($fs, "This only shows up on the CLI...\n");
fclose($fo);
fclose($fs);
?>
Using the CLI you'll see:
 You can see this with the CLI and Apache.
 This only shows up on the CLI...
Using the Apache SAPI you'll see:
 You can see this with the CLI and Apache.


Change Language


Follow Navioo On Twitter
History of PHP and related projects
Migrating from PHP 5.1.x to PHP 5.2.x
Migrating from PHP 5.0.x to PHP 5.1.x
Migrating from PHP 4 to PHP 5
Migrating from PHP 3 to PHP 4
Migrating from PHP/FI 2 to PHP 3
Debugging PHP
Configure options
php.ini directives
List of Supported Timezones
Extension Categorization
List of Function Aliases
List of Reserved Words
List of Resource Types
List of Supported Protocols/Wrappers
List of Available Filters
List of Supported Socket Transports
PHP type comparison tables
List of Parser Tokens
Userland Naming Guide
eXTReMe Tracker