如何防止用户使用PHP下载选项下载源代码文件,我可以在我的web应用程序中下载pdf文件?

如何防止用户使用PHP下载选项下载源代码文件,我可以在我的web应用程序中下载pdf文件?,php,security,extjs,Php,Security,Extjs,下载.php <?php $file = $_GET['file']; if(file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Tra

下载.php

<?php 
$file = $_GET['file'];
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;
}
?>
如果我在url中输入“Download.php?file=../web.config”,则会下载web.config文件。我想阻止直接下载源代码。下载选项用于下载存储在主目录中pdf文件夹中的pdf文件


请帮忙

在这里,你已经被一个非常糟糕的设计决策弄得不知所措

你可以考虑:

  • 确保请求的文件以.pdf结尾
  • 确保正在读取的文件以.pdf结尾
  • 删除文件参数包含
    的所有请求。

  • <> Given Download.php不希望确保请求者被认证,我建议您的PDF文档可以在Web可访问的目录中直接链接,而不是创建一个可能危及服务器的攻击向量。

    在Web.CONFIG,

    中使用此。
    <authorization>
        <allow users="user1, user2"/>
        <deny users=”?”/>
    </authorization>
    


    不要让代码绕过这一点,正如Michael M所说。

    alexJoe,如果您手动运行PHP文件,请手动创建URL并运行它,没有JavaScript,它也会这样做吗?这有帮助吗?如果这对你没有帮助的话,我不想投票表决这个问题已经解决了(又称关闭)。另一个脚本是问题吗?很抱歉我不明白。不,如果有人只是更改url,比如“Download.php?file=../web.config”,我有没有办法阻止下载源代码文件。谢谢Michael,太棒了!如果请求的文件是pdf、csv,我已经添加了验证。
    <authorization>
        <allow users="user1, user2"/>
        <deny users=”?”/>
    </authorization>