【问题标题】:Validating a downloading file验证下载文件
【发布时间】:2014-08-08 10:35:21
【问题描述】:

我在服务器中有一些文件可供用户下载,但用户必须登录才能下载这些文件,因此文件格式为 rar 或 zip。在 php 中下载文件之前,有什么方法可以验证使用是否已记录?

<?php
session_start();

if (isset($_COOKIE['username']) AND isset($_COOKIE['hash']) {
    if (isset($_SESSION['url']) {
        Header("Location:".$_SESSION['url']);
    }
}

?>

【问题讨论】:

  • 在向用户提供任何其他需要登录的内容之前验证用户已经登录的方式相同。
  • 我可以通过 php 文件进行验证,但如何验证 rar 文件?
  • @Shn 提示:php 文件不必返回 html 文档。
  • 就目前而言,这个问题太宽泛了。请详细说明您当前如何检查用户是否已登录以提供其他内容,以及您要提供的文件存储在何处。将此信息编辑到您的问题中。
  • @Sumurai8 我已经用我使用的代码更新了问题,我使用 cookie 验证用户

标签: javascript php .htaccess


【解决方案1】:

是的,下面是使用方法 step1:防止zip文件直接访问

命令允许,拒绝 否认一切

第二步:

<?php
function output_file($file, $name, $mime_type='')
{
 if(!is_readable($file)) die('File not found or inaccessible!');
 $size = filesize($file);
 $name = rawurldecode($name);
 $known_mime_types=array(
    "htm" => "text/html",
    "exe" => "application/octet-stream",
    "zip" => "application/zip",
    "doc" => "application/msword",
    "jpg" => "image/jpg",
    "php" => "text/plain",
    "xls" => "application/vnd.ms-excel",
    "ppt" => "application/vnd.ms-powerpoint",
    "gif" => "image/gif",
    "pdf" => "application/pdf",
    "txt" => "text/plain",
    "html"=> "text/html",
    "png" => "image/png",
    "jpeg"=> "image/jpg"
 );

 if($mime_type==''){
     $file_extension = strtolower(substr(strrchr($file,"."),1));
     if(array_key_exists($file_extension, $known_mime_types)){
        $mime_type=$known_mime_types[$file_extension];
     } else {
        $mime_type="application/force-download";
     };
 };

 //turn off output buffering to decrease cpu usage
 @ob_end_clean(); 

 // required for IE, otherwise Content-Disposition may be ignored
 if(ini_get('zlib.output_compression'))
 ini_set('zlib.output_compression', 'Off');
 header('Content-Type: ' . $mime_type);
 header('Content-Disposition: attachment; filename="'.$name.'"');
 header("Content-Transfer-Encoding: binary");
 header('Accept-Ranges: bytes');

 // multipart-download and download resuming support
 if(isset($_SERVER['HTTP_RANGE']))
 {
    list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2);
    list($range) = explode(",",$range,2);
    list($range, $range_end) = explode("-", $range);
    $range=intval($range);
    if(!$range_end) {
        $range_end=$size-1;
    } else {
        $range_end=intval($range_end);
    }

    $new_length = $range_end-$range+1;
    header("HTTP/1.1 206 Partial Content");
    header("Content-Length: $new_length");
    header("Content-Range: bytes $range-$range_end/$size");
 } else {
    $new_length=$size;
    header("Content-Length: ".$size);
 }

 /* Will output the file itself */
 $chunksize = 1*(1024*1024); //you may want to change this
 $bytes_send = 0;
 if ($file = fopen($file, 'r'))
 {
    if(isset($_SERVER['HTTP_RANGE']))
    fseek($file, $range);

    while(!feof($file) && 
        (!connection_aborted()) && 
        ($bytes_send<$new_length)
          )
    {
        $buffer = fread($file, $chunksize);
        echo($buffer); 
        flush();
        $bytes_send += strlen($buffer);
    }
 fclose($file);
 } else
 //If no permissiion
 die('Error - can not open file.');
 //die
die();
}
//Set the time out
set_time_limit(0);

//path to the file
$file_path='files/'.$_REQUEST['filename'];


//Call the download function with file path,file name and file type

if($_SESSION['userloggedin']){
output_file($file_path, ''.$_REQUEST['filename'].'', 'text/plain');
}else{
    echo "login";
}
?>

【讨论】:

  • 你可以使用readile()提供zip作为下载。那么你不需要那个字节读取结构
  • 是的,但你可以使用上面的代码下载所有类型的文件,如 zip 或其他文件
  • 这段代码安全吗? fseek(..) 不会阻止您设置指针 after 文件结束的位置,feof(...) 的文档说它只返回 true at EOF,不一定在 EOF 之后。
  • @gaurav 好吧,不是真的。它不会检查您请求的文件名,因此您可以转储此文件或此目录中的任何其他文件以及父目录中的任何其他文件的内容而不会出现问题。经过一些测试,fseek()feof() 可以正常工作。 fread(..) 确实默默地纠正了这个问题,并且脚本似乎检查了 filesize(..) 以确保它不会尝试读取文件之外的内容。
  • @Sumurai8 是的,如果需要任何修改,请不要客气。
【解决方案2】:

您可以像您提供的任何其他内容一样执行此操作。 php 文件不必返回text/html 文档。它可以提供任何数据,包括图像、pdf 或可执行文件。您只需使用正确的标题即可。

<?php
if(isset($_COOKIE['username']) && isset($_COOKIE['hash'])
{
  //The user is logged in... we have no clue if the user is allowed to download the file
  header( "Content-Type: image/jpeg" );
  header( "Cache-Control: private, max-age=0, no-cache" );

  file_get_contents( "images/image.jpg" );
  exit();
} else {
  header('HTTP/1.0 403 Forbidden');
  exit();
}

缓存控制标头用于防止客户端和您的服务器之间的服务器缓存此请求。 ISP 有时这样做是为了更快地提供公共文件。内容类型标头应根据您提供的文件设置为正确的 mime 类型。您可以在 wikipedia 上找到常见 mime 类型的列表。

重要

此答案使用“file_get_contents(...)”。您应该始终确保传递给此函数的 url 是健全的。这意味着:它应该指向一个现有的文件,并且它应该只指向一个你真正想要提供的文件。如果有人通过 '../../../.htaccess' 你不想不小心将该文件的内容泄露给用户!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    相关资源
    最近更新 更多