PHP偽協(xié)議是一種特殊的URL格式,允許開(kāi)發(fā)者以不同于傳統(tǒng)文件路徑訪問(wèn)和操作資源。以下是一些常見(jiàn)的PHP偽協(xié)議及其詳細(xì)介紹:
常見(jiàn)的PHP偽協(xié)議
1. **file://**
- **用途**:訪問(wèn)本地文件系統(tǒng)。
- **示例**:`file:///path/to/file.txt`。
2. **http:// 和 https://**
- **用途**:訪問(wèn)HTTP(S) URLs。
- **示例**:`http://example.com`。
3. **ftp://**
- **用途**:訪問(wèn)FTP(S) URLs。
- **示例**:`ftp://example.com/file.txt`。
4. **php://**
- **用途**:訪問(wèn)各種輸入/輸出流(I/O streams)。
- **子協(xié)議**:
- `php://stdin`、`php://stdout`、`php://stderr`:標(biāo)準(zhǔn)輸入、輸出和錯(cuò)誤流。
- `php://input`:訪問(wèn)請(qǐng)求的原始數(shù)據(jù)流。
- `php://memory` 和 `php://temp`:在內(nèi)存或臨時(shí)文件中讀寫(xiě)數(shù)據(jù)。
5. **php://filter**
- **用途**:在數(shù)據(jù)流打開(kāi)時(shí)應(yīng)用過(guò)濾器。
- **示例**:`php://filter/read=convert.base64-encode/resource=example.txt`。
6. **zlib://**
- **用途**:壓縮流。
- **示例**:`zlib://path/to/file.gz`。
7. **data://**
- **用途**:內(nèi)聯(lián)數(shù)據(jù)流(RFC 2397)。
- **示例**:`data://text/plain;base64,SGVsbG8sIFdvcmxkIQ==`。
8. **glob://**
- **用途**:查找匹配的文件路徑模式。
- **示例**:`glob://*.txt`。
9. **phar://**
- **用途**:訪問(wèn)PHP歸檔文件(PHAR)。
- **示例**:`phar://path/to/archive.phar/file.php`。
10. **ssh2://**
- **用途**:通過(guò)SSH2協(xié)議訪問(wèn)資源。
- **示例**:`ssh2.sftp://user:[email protected]/path/to/file`。
11. **rar://**
- **用途**:訪問(wèn)RAR壓縮包。
- **示例**:`rar://path/to/archive.rar/file.txt`。
12. **ogg://**
- **用途**:訪問(wèn)OGG音頻流。
- **示例**:`ogg://path/to/file.ogg`。
13. **expect://**
- **用途**:處理交互式流。
- **示例**:`expect://command`。
示例代碼
以下是一個(gè)使用 `php://filter` 偽協(xié)議的示例代碼:
```php
echo file_get_contents("php://filter/read=convert.base64-encode/resource=data://text/plain,Hello World!");
// 輸出:SGVsbG8gV29ybGQh
?>
```
- 某些偽協(xié)議可能需要特定的PHP配置選項(xiàng)(如 `allow_url_fopen` 和 `allow_url_include`)啟用。