Php 允许用户从服务器查看和下载文件

Php 允许用户从服务器查看和下载文件,php,web,download,Php,Web,Download,我希望用户能够在选择下载文件之前在网站上查看文件(我需要进一步研究,PDF/doc viewer、vid player等)。对合适的方法有什么建议吗 但主要的问题是允许他们下载文件 我在另一篇帖子上读到: <?php $file = 'monkey.gif'; if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/o

我希望用户能够在选择下载文件之前在网站上查看文件(我需要进一步研究,PDF/doc viewer、vid player等)。对合适的方法有什么建议吗

但主要的问题是允许他们下载文件

我在另一篇帖子上读到:

<?php
$file = 'monkey.gif';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>

是合适的,但是我将从数据库中提取文件名。我正在考虑使用一个标准的下载页面,以便
download.php?filename=thisFileToDownload
可以传递文件名

另一个问题是,有不同的文件类型,如图像、视频、excel、文档和PDF等。我是否会遇到任何问题


感谢您的帮助

如果我转到
download.php?filename=/etc/passwd
文件名附带了一个唯一的id,希望普通joe将文件名弹出到url中不会那么简单。在使用相同技术的另一个页面中键入上述内容,会返回一条消息:etcpasswd
是否有可能遇到任何问题?
。。。也许如果你希望在视频中寻找