您可以使用“php 下载处理程序”执行此操作:
您可以使用这样的方法将文件内容和文件信息头返回给用户浏览器,只需确保在此之前没有输出任何其他内容。
我建议你把它放到单独的文件中,例如调用它download.php。
function returnFile( $filename ) {
    // Check if file exists, if it is not here return false:
    if ( !file_exists( $filename )) return false;
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    // Suggest better filename for browser to use when saving file:
    header('Content-Disposition: attachment; filename='.basename($filename));
    header('Content-Transfer-Encoding: binary');
    // Caching headers:
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    // This should be set:
    header('Content-Length: ' . filesize($filename));
    // Clean output buffer without sending it, alternatively you can do ob_end_clean(); to also turn off buffering.
    ob_clean();
    // And flush buffers, don't know actually why but php manual seems recommending it:
    flush();
    // Read file and output it's contents:
    readfile( $filename );
    // You need to exit after that or at least make sure that anything other is not echoed out:
    exit;
}
将其扩展为基本用途:
// Added to download.php
if (isset($_GET['file'])) {
    $filename = '/home/username/public_files/'.$_GET['file'];
    returnFile( $filename );
}
警告:
这是一个基本的例子,并没有考虑到用户可能会试图利用一些$_GET没有得到适当清理的邪恶优势。
这基本上意味着passwd如果某些条件适用,用户可以例如检索文件或其他一些敏感信息。
例如,检索/etc/passwd:
只需将浏览器指向http://server.com/download.php?file=../../../etc/passwd服务器即可返回该文件。因此,在实际使用之前,您应该了解如何正确检查和清理任何用户提供的参数。