简体   繁体   English

如何阻止 URL 直接访问文件夹中的所有文件,但仍允许用户下载?

[英]How can I block direct URL access to all file in a folder, but still allow it to be downloaded by users?

i have a folder with name uploads and have files i want to block direct download i readed an used this link PHP: How can I block direct URL access to a file, but still allow it to be downloaded by logged in users?我有一个名称为上传的文件夹,并且有文件我想阻止直接下载我阅读了使用此链接PHP:如何阻止 URL 直接访问文件,但仍允许登录用户下载它? in stackoverflow but it just not work and just download a Broken file在stackoverflow中,但它不起作用,只需下载一个损坏的文件

this is my code .htaccess file这是我的代码 .htaccess 文件

 Order Deny,Allow
    Deny from all

php file php 文件






  <?php



if( !empty( $_REQUEST['name'] )&& !empty( $_REQUEST['user'] ) && !empty( $_REQUEST['pass'] ) )
{
 include 'include/DbHandler.php';
$db = new DbHandler();
   $user= $_REQUEST['user'];
  $pass= $_REQUEST['pass'];
  $file_name= $_REQUEST['name'];
 // file name like this : hps.jpg
      $nest = $db->checkuserpass($user,$pass,$file_name);
 if ($nest!=2) 
  {
  /// user have premision for download 

    function getExtension($file){
        preg_match('/\.[^\.]+$/i', $file, $ext);
        return $ext[0];
    }

    //File type 
    $file_type = getExtension($file_name);



    header('Content-Description: File Transfer');
    //header('Content-Type: application/octet-stream'); 
    header('Content-Type: '.$file_type);
    header('Content-Disposition: attachment; filename='.$file_name);
    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_name"));
    ob_clean();
    flush();
    readfile("$file_name");
    exit;


  }
}
 ?>

Instead of denying access by .htaccess file you could save your file at a folder outside public side of your server, that is, outside www directory.除了拒绝 .htaccess 文件的访问之外,您还可以将文件保存在服务器公共端之外的文件夹中,即 www 目录之外。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP:如何阻止对文件的直接URL访问,但仍允许登录用户下载文件? - PHP: How can I block direct URL access to a file, but still allow it to be downloaded by logged in users? 我如何使用htaccess拒绝从URL直接访问服务器文件夹中的所有php文件和某些html文件? - How can i deny direct access from url to all php file and some html in my server folder using htaccess? 如何阻止对目录中图像的直接访问,但允许PHP显示它们? - How can I block direct access to images in a directory but allow PHP to display them? 阻止通过HTTP直接访问文件,但允许php脚本访问 - Block direct access to a file over http but allow php script access 阻止直接访问PHP文件,仅允许本地javascript访问它 - block direct access to PHP file and only allow local javascript to access it 阻止直接访问文件,但允许通过jquery加载功能进行访问 - block direct access to file but allow access through jquerys load function 阻止来自直接 URL 的访问,但不阻止来自 mysql 数据库中的用户的访问 - Block access from direct URL, but not from users in mysql database 允许 PHP 脚本访问文件夹中的 PDF - 但阻止直接 URL 引用 - Allow a PHP script access to PDFs in a folder - but prevent direct URL references 拒绝直接访问文件夹或文件,但允许index.php访问它 - Deny direct access to a folder or file, but allow index.php to access it 如何阻止对我的 JavaScript 文件的直接访问? - How can I block direct access to my JavaScript files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM