附录 L. 支持的协议/封装协议列表

以下是 PHP 内置用于文件系统函数例如 fopen()copy() 的 URL 风格协议列表。除了这些封装协议之外,从 PHP 4.3 起,还可以用 PHP 脚本和 stream_wrapper_register() 写自己的封装协议。

文件系统

所有版本的 PHP。自 PHP 4.3.0 以来明确使用 file://

  • /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

file:// 是 PHP 默认的描述本地文件系统的封装协议。指定相对路径 (相对路径名不开始于 /、\、\\ 或 Windows 盘符) 时,该路径是相对于当前的工作目录。在很多情况下工作目录就是当前执行的脚本所在的目录,除非它被改变。使用 CLI sapi 时,默认是调用该脚本时终端所处的目录。

某些函数,例如 fopen()file_get_contents(),会自动搜索 include_path 作为相对路径。

表格 L-1. 封装协议摘要

属性支持
受限于 allow_url_fopen
允许读取
允许写入
允许附加
允许同时读写
支持 stat()
支持 unlink()
支持 rename()
支持 mkdir()
支持 rmdir()


add a note add a note User Contributed Notes
ben dot johansen at gmail dot com
26-Oct-2006 05:57
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');
ben dot johansen at gmail dot com
30-Aug-2006 02:02
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
ben dot johansen at gmail dot com
29-Aug-2006 03:33
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);
heitorsiller at uol dot com dot br
07-Jul-2006 10:55
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.
opedroso at NOSPAMswoptimizer dot com
13-Apr-2006 02:07
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.
nyvsld at gmail dot com
28-Nov-2005 02:28
php://stdin supports fseek() and fstat() function call,
while php://input doesn't.
drewish at katherinehouse dot com
25-Sep-2005 02:50
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.
chris at free-source dot com
27-Apr-2005 03:52
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).
nargy at yahoo dot com
24-Sep-2004 06:16
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);
aidan at php dot net
27-May-2004 06:34
The contants:

* STDIN
* STDOUT
* STDERR

Were introduced in PHP 4.3.0 and are synomous with the fopen('php://stdx') result resource.
lupti at yahoo dot com
29-Nov-2003 06:04
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.
sam at bigwig dot net
15-Aug-2003 11:02
[ 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.