php - 保护文件下载在服务器上的文件夹中 &.htaccess 可见

标签 php .htaccess

我正在使用如下的 php 文件代码列出目录中的文件,用户将从中检查所需文件并将它们下载为 zip 文件

我有两个问题

1) 如何保护文件免于使用 url 直接下载我希望文件仅通过 downloadlist.php 的表单操作下载(如果我在 .htaccess 中保留 denyall,下载的文件已损坏)

2) 这段代码还在下载列表中显示了 .htaccess(所以我怀疑他们是只列出 Docs、xls、pdf 的方法)

如果需要我可以提供downloadlist.php

<?php
function listDir($dirName) 
{  
    ?><form name="filelist" action="downloadList.php" method="POST"><?php echo "\n";
    if ($handle = opendir($dirName)) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") { ?>    <input type=checkbox name="file[]" value="<?php  echo "$file";?>"><?php  echo "$file"; ?><br><?php  echo "\n";
            }
        }
    closedir($handle);
    }
    ?><br><input type="submit" name="formSubmit" value="Zip and download" /></form><?php
}
listDir('./fold');  ?>

下载列表.php

<?php
// function download($file) downloads file provided in $file
function download($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;
}

$files = $_POST['file'];
if(empty($files)) 
{
    echo("You haven't selected any file to download.");
} 
else 
{
    $zip = new ZipArchive();
    $filename = time() . "archive.zip"; //adds timestamp to zip archive so every file has unique filename
    if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) { // creates new zip archive
            exit("Cannot open <$filename>\n");
    }
    $N = count($files);
    for($i=0; $i < $N; $i++)
    {
      $zip->addFile($files[$i], $files[$i]); //add files to archive
    }
    $numFiles = $zip->numFiles;
    $zip->close();

    $time = 8; //how long in seconds do we wait for files to be archived.
    $found = false;
    for($i=0; $i<$time; $i++){

     if($numFiles == $N){   // check if number of files in zip archive equals number of checked files
        download($filename);
        $found = true;
        break;
     }
     sleep(1); // if not found wait one second before continue looping
 }

 if($found) { }
     else echo "Sorry, this is taking too long";
 } ?>

最佳答案

将此行添加到 fold 目录中的 .htaccccess 文件中!

deny from all

并编辑这一行:

  $zip->addFile($files[$i], $files[$i]); //add files to archive

收件人:

  $zip->addFile('./fold/'.$files[$i], $files[$i]); //add files to archive

listDir函数:

  <?php

function listDir($dirName) 
{  
    $forbidden_files=array('.htaccess','.htpasswd');
    $allow_ext=array('.gif','.png','.bmp','.jpeg','.jpg','.pdf','.doc','.docx','.xls','.xlsx','.php');
    ?><form name="filelist" action="downloadList.php" method="POST"><?php echo "\n";
    if ($handle = opendir($dirName)) {
        while (false !== ($file = readdir($handle))     ) {
            $allowed=(strpos($file,'.')!==false  &&  in_array(substr($file,strpos($file,'.')) ,$allow_ext ));

           if ($file != "." && $file != ".."  && $allowed ) { ?>    <input type=checkbox name="file[]" value="<?php  echo "$file";?>"><?php  echo "$file"; ?><br><?php  echo "\n";
            }
        }
    closedir($handle);
    }
    ?><br><input type="submit" name="formSubmit" value="Zip and download" /></form><?php
}
listDir('./fold');  ?>

关于php - 保护文件下载在服务器上的文件夹中 &.htaccess 可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18291992/

相关文章:

php - 将 2 个 SQL 查询合并为一个 - 显示总销售额和净销售额

.htaccess - 如何使用 php 在我的目录中创建友好的 url

php - MySQL在一个ID下连接多行

php - 尝试向文本输入添加字符限制,但 javascript 和 php 显示不同的字符串长度

php - 在 yii2 中使用 memcache 缓存一个值

apache - 在没有 php 或其他任何仅使用 .htaccess 的情况下为单个 .html 页面强制使用 SSL

.htaccess - 如何使用 htaccess 和 mod_rewrite 在内部重定向所有内容?

.htaccess - 慢: no character set specified in their HTTP headers

.htaccess - 从 url 中删除参数

php - 在 PHP PostgreSQL 查询结果中找到或未找到结果时显示消息