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