2012-01-24 76 views
0

即时通讯使用的是基于PHP登录验证机制,允许/限制访问我的网站的某些部分(文件夹模块1模块2,等等),但我有限制对文件的访问问题。 我用文件夹(下面检查)来承载一些可下载的文件。指向这些文件的链接出现在index.php中(托管在根目录目录中)。但是,如果由于某种原因,非授权用户获得文档中的文件的URL,他将能够下载它。Web托管文件授权

/ 
/documents/ 
/module1/ 
/module2/ 

PS:因为这是一个内部网站我限制了访问文件通过IP地址,但仍然有一个小的机会,有人使用PC与允许的IP地址和他在文档的URL 。

回答

0

使用某种代理PHP脚本,该脚本将为用户提供文件而不提供真实的源位置。

用户会再看看http://yourdomain.com/download.php?file=mydoc.docx

的实际路径仍是/documents/userid/2342/mydoc.docx或什么都你的结构是什么样子。

然后让你的download.php文件由提供服务的文件:

<?php 
// Validate the user here 

// Set document root 
$root = 'documents/userid/'.$userID.'/'; 

// Requested file 
$file = $_GET['file']; 

// Validate 
if (file_exists($root . $file)) 
{ 
    header("Pragma: public"); 
    header("Expires: 0"); 
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
    header("Cache-Control: private", false); 
    header("Content-Type: application/force-download"); 
    header("Content-Disposition: attachment; filename=\"".basename($file)."\";"); 
    header("Content-Transfer-Encoding: binary"); 
    header("Content-Length: ".filesize($root . $file)); 

    ob_clean(); 
    flush(); 
    readfile($root . $file); 
} 
else { echo "File not found"; } 
?> 

See more here