在PHP中,可以使用header()函数来实现文件下载功能。
具体步骤如下:
以下是一个简单的PHP文件下载函数的示例代码:
php
function downloadFile($file) {
// 打开要下载的文件
$fp = fopen($file, 'rb');
// 设置响应头,指定文件的类型和大小
header("Content-Type: application/octet-stream");
header("Content-Length: " . filesize($file));
header("Content-Disposition: attachment; filename=\"" . basename($file) . "\"");
// 将文件内容输出到客户端
fpassthru($fp);
// 关闭文件句柄
fclose($fp);
}
使用方法:
调用downloadFile()函数,并传入要下载的文件路径作为参数即可。
php
downloadFile('/path/to/file');
注意事项: