以下是一个简单的PHP实例,演示了如何使用PHP控制下载文件的过程。这个例子中,我们将创建一个简单的HTML表单,用户可以通过该表单选择一个文件进行下载。
```php

// 检查是否有文件被选中
if (isset($_GET['file'])) {
// 定义文件路径
$file_path = $_GET['file'];
// 检查文件是否存在
if (file_exists($file_path)) {
// 获取文件扩展名
$file_extension = strtolower(pathinfo($file_path, PATHINFO_EXTENSION));
// 设置合适的Content-Type
switch ($file_extension) {
case 'pdf':
$content_type = 'application/pdf';
break;
case 'doc':
case 'docx':
$content_type = 'application/msword';
break;
case 'txt':
$content_type = 'text/plain';
break;
default:
$content_type = 'application/octet-stream';
}
// 设置HTTP头信息
header('Content-Type: ' . $content_type);
header('Content-Length: ' . filesize($file_path));
header('Content-Disposition: attachment; filename="





