PHP 封装协议(wrapper)介绍
1. file://
- 用途:用于访问本地文件系统中的文件,是默认的 wrapper,在操作本地文件时,
file:// 前缀通常可以省略。
- 示例:
1 2 3
| $filePath = 'file:///var/www/html/test.txt'; $fileContent = file_get_contents($filePath); echo $fileContent;
|
2. http://
- 用途:用于通过 HTTP 协议访问远程服务器上的资源。
- 示例:
1 2 3
| $url = 'http://example.com/data.txt'; $content = file_get_contents($url); echo $content;
|
- 注意事项:使用此 wrapper 需要确保服务器允许 HTTP 请求,并且 PHP 配置中
allow_url_fopen 选项为 On。
3. ftp://
- 用途:用于通过 FTP 协议访问远程 FTP 服务器上的文件。
- 示例:
1 2 3
| $ftpUrl = 'ftp://username:password@ftp.example.com/path/to/file.txt'; $ftpContent = file_get_contents($ftpUrl); echo $ftpContent;
|
- 注意事项:使用此 wrapper 需要服务器支持 FTP 访问,并且要提供正确的用户名和密码。
4. php://
这是一个功能强大的 wrapper,有多种不同的流可以使用:
php://stdin、php://stdout、php://stderr:分别对应标准输入、标准输出和标准错误输出流。常用于命令行脚本。
1 2 3
| $input = fgets(STDIN); echo "You entered: ". $input;
|
php://input:用于访问原始的 POST 数据,特别是在处理非 application/x-www-form-urlencoded 格式的 POST 请求时很有用。
1 2
| $rawPostData = file_get_contents('php://input'); echo $rawPostData;
|
php://output:用于向输出缓冲区写入数据,类似于 echo 和 print。
1 2 3
| $fp = fopen('php://output', 'w'); fwrite($fp, 'Writing to output buffer'); fclose($fp);
|
5. zlib://
- 用途:用于读取和写入经过 zlib 压缩的文件,通常是
.gz 格式的文件。
- 示例:
1 2 3
| $gzFile = 'zlib:///path/to/file.gz'; $content = file_get_contents($gzFile); echo $content;
|
6. data://
- 用途:允许直接在 URL 中嵌入数据,通常用于创建临时内容或测试。
- 示例:
1 2 3 4 5
| $text = 'This is a test'; $encodedText = base64_encode($text); $url = 'data://text/plain;base64,'.$encodedText; $content = file_get_contents($url); echo $content;
|
7. glob://
- 用途:用于匹配一组文件,使用通配符(如
*、? 等)来指定文件模式。
- 示例:
1 2 3 4
| $files = glob('glob:///var/www/html/*.txt'); foreach ($files as $file) { echo $file. "\n"; }
|
8. phar://
- 用途:用于访问 PHP Archive(PHAR)文件,PHAR 文件是一种将多个 PHP 文件打包成单个文件的方式,便于分发和部署。
- 示例:
1 2 3
| $pharFile = 'phar:///path/to/myapp.phar/file.php'; $pharContent = file_get_contents($pharFile); echo $pharContent;
|
9. ssh2://
- 用途:提供了通过 SSH 协议进行远程操作的功能,有不同的子流,如
ssh2.shell、ssh2.exec、ssh2.tunnel 等。
- 示例(
ssh2.exec):
1 2 3 4 5
| $connection = ssh2_connect('example.com', 22); ssh2_auth_password($connection, 'username', 'password'); $stream = fopen('ssh2.exec://'.$connection.'/bin/ls -l', 'r'); $output = stream_get_contents($stream); echo $output;
|
10. rar://
- 用途:用于访问 RAR 压缩文件中的内容。
- 示例:
1 2 3
| $rarFile = 'rar:///path/to/archive.rar#file.txt'; $rarContent = file_get_contents($rarFile); echo $rarContent;
|
11. ogg://
- 用途:用于处理 Ogg Vorbis 音频文件。可以读取和操作 Ogg 格式的音频流。不过在 PHP 中使用相对较少,更多用于特定的多媒体处理场景。
- 示例:通常结合音频处理库来使用,例如读取 Ogg 文件的元数据等。
1 2 3
| $oggFile = 'ogg:///path/to/audio.ogg';
|
12. expect://
- 用途:用于与交互式程序进行通信,通过模拟用户输入和响应来自动化操作。
- 示例:
1 2 3 4
| $handle = fopen('expect://ssh user@example.com', 'r+'); fwrite($handle, "password\n"); $output = stream_get_contents($handle); echo $output;
|
13. filter://
- 用途:用于对数据流进行过滤和转换,例如对文件内容进行编码转换、去除 HTML 标签等。
- 示例(去除 HTML 标签):
1 2 3
| $html = '<p>Hello, <b>World!</b></p>'; $filtered = file_get_contents('filter://strip_tags/resource=data://text/plain,'. urlencode($html)); echo $filtered;
|
评论区
欢迎你留下宝贵的意见