简体   繁体   English

所有文件仅通过 index.php 下载并拒绝从浏览器直接访问它们

[英]All files download only through index.php and deny direct access to them from browser

I have the following structure in my website:我的网站有以下结构:

  • index.php index.php
  • files(directory) ---> file1.pdf文件(目录)---> file1.pdf

How can I prevent the direct access to the file (eg https://example.com/files/file1.pdf ) and allow the file to be downloaded from within the displayed web page for login users?如何防止直接访问文件(例如https://example.com/files/file1.pdf )并允许从显示的 Z2567A5EC9705EB7AC2C984033E061 页面中下载文件登录用户?

here is the php code for the index which reads the files from the directory:这是从目录中读取文件的索引的 php 代码:

<?php
        include('session.php');

        $path    = './files';
        $files = scandir($path);
        $files = preg_grep("/^(\.|\.\.|index\.php|\.htaccess)$|.php$|\.meta\.js$/",scandir($path), PREG_GREP_INVERT);
        foreach($files as $file){
          echo '<div>';
          echo "<a href='$file' >$file</a>";
          echo "</div>";
        }
        ?>
  1. Create an .htaccess in files and set deny all.在文件中创建一个 .htaccess 并设置全部拒绝。

    order deny,allow deny from all命令拒绝,允许所有人拒绝

  2. Create downloader.php and update your download link urls like创建 downloader.php 并更新您的下载链接网址,例如

domain.com/downloader.php?file=filename domain.com/downloader.php?file=filename

Code:代码:

    <?php 

if(!isset($_GET['file']))
{
    die('File Request Not found.');
}
if(!file_exists('files/'.$_GET['file']))
{
    die('File not exists. File name ='.$_GET['file']);
}
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$_GET['file']."\""); 
echo readfile('files/'.$_GET['file']);
?>

. .

AddHandler application/x-httpd-php .html

<FilesMatch "\.(?i:gif|jpe?g|png)$">

order deny,allow

Deny from all

</FilesMatch>

Usage = These rules will allow you to download the files only from index.html and will deny the direct access to them from the browser.用法 = 这些规则将只允许您从 index.html 下载文件,并拒绝从浏览器直接访问它们。

So here is how i manged to solve the problem:所以这是我设法解决问题的方法:

in the .htaccess added the rules:在 .htaccess 中添加了规则:

<FilesMatch "\.(?i:pdf|jpe?g|png)$">

order deny,allow

Deny from all

</FilesMatch>

So now no one can access the files via direct link from browser.所以现在没有人可以通过浏览器的直接链接访问这些文件。

then added the following code to the downloader.php (of course still needs to be linked to the session to allow logged in users only):然后将以下代码添加到下载器中。php (当然仍然需要链接到 session 以仅允许登录用户):

<?php

if(isset($_GET['path']))
{
//Read the filename
$filename = $_GET['path'];
//Check the file exists or not
if(file_exists($filename)) {

//Define header information
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Cache-Control: no-cache, must-revalidate");
header("Expires: 0");
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header('Content-Length: ' . filesize($filename));
header('Pragma: public');

//Clear system output buffer
flush();

//Read the size of the file
readfile($filename);

//Terminate from the script
die();
}
else{
echo "File does not exist.";
}
}
else
echo "Filename is not defined."
?>

and made a little change to the index.php:并对 index.php 做了一点改动:

 echo "<a href=./downloader.php?path='$pathOF/$file' class='pdfl'>$file</a>";

and all working fine, just a little bit delay when requesting a file download, maybe it is a hosting issue or may be the downloader not sure, please let me know if there is a better way to do this.一切正常,请求文件下载时只是有点延迟,可能是主机问题或者可能是下载器不确定,如果有更好的方法,请告诉我。

Regards问候

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM