2014-03-31 188 views
0

我有一个joomla网站。并有一些pdf文件到网站的根目录。阻止访客用户直接访问PDF链接

有没有一种方法可以保护直接访问的客人(公共)用户的PDF文件?并允许注册用户?

我试过htaccess(拒绝),但注册用户也无法直接查看pdf .. 已搜索但没有发现任何关于此..请有人帮忙。

谢谢

+1

您无法检测到用户已登录或没有使用htaccess文件,它将需要PHP。首先编写脚本来检测用户是否已登录,在根目录中获取PDF文件阵列,检测文件名是否在URL中,然后进行相应的重定向。这些任务中的每一个都可以通过简单的Google搜索找到。如果遇到特定的编码问题,请发布另一个问题。 – Lodder

回答

0

在.htacess文件,你必须添加以下代码

所有

否认它应该在PDF文件所在的文件夹发票下进行定位。

如果您使用全部拒绝,那么文件不具有htacess文件所在的特定目录的下载访问权限。

要允许注册用户下载访问,必须调用以下控制器而不是直接文件路径url。

URL例如: - www.example.com/index.php?option=com_temp &任务= temp.downloadmypdf &文件=文件名

public function downloadmypdf(){ 

    $user = JFactory::getUser(); 
    $getfile = JRequest::getVar('file'); 

    if($user->get('id') == 0){ 
     die('permission denied'); 
    } 

    $link = "/invoice/".$getfile.".pdf"; 
    $file = JPATH_SITE.$link; 

    header("Content-Type: application/octet-stream"); 

    $filename = $getfile.'.pdf'; 

    header("Content-Disposition: attachment; filename=".urlencode($filename)); 
    header("Content-Type: application/force-download"); 
    header("Content-Type: application/octet-stream"); 
    header("Content-Type: application/download"); 
    header("Content-Description: File Transfer"); 
    header("Content-Length: " . filesize($file)); 
    flush(); // this doesn't really matter. 

    $fp = fopen($file, "r"); 

    while (!feof($fp)) 
    { 
     echo fread($fp, 65536); 
     flush(); // this is essential for large downloads 
    } 
    fclose($fp); 

    JFactory::getApplication()->close(); 
} 

现金去Ashli​​n rejo。

+0

谢谢。你能告诉我在哪里可以添加这个功能吗?在模板的index.php中? –

+0

您能否告诉我如何通过手动添加pdf文件或者您是否使用任何组件来保存文件。 – Selvaraj

+0

不,这个pdf是在“root/files /”文件夹中手动添加的。 –

1

创建一个名为的download.php

文件中添加以下代码的download.php文件用PHP标签封装

define('_JEXEC', 1); 
define('DS', DIRECTORY_SEPARATOR); 

if (file_exists(dirname(__FILE__) . '/defines.php')) { 
    include_once dirname(__FILE__) . '/defines.php'; 
} 

if (!defined('_JDEFINES')) { 
    define('JPATH_BASE', dirname(__FILE__)); 
    require_once JPATH_BASE.'/includes/defines.php'; 
} 

require_once JPATH_BASE.'/includes/framework.php'; 

// Mark afterLoad in the profiler. 
JDEBUG ? $_PROFILER->mark('afterLoad') : null; 

// Instantiate the application. 
$app = JFactory::getApplication('site'); 

// Initialise the application. 
$app->initialise(); 

$user = JFactory::getUser(); 
$getfile = JRequest::getVar('file',null,'get','string'); 

if($getfile){ 

    if($user->get('id') == 0){ 
     die('permission denied'); 
    } 


    $link = "/files/".$getfile.".pdf"; // Locate the pdf file 
    $file = JPATH_SITE.$link; 

    header("Content-Type: application/octet-stream"); 

    $filename = $getfile.'.pdf'; 

    header("Content-Disposition: attachment; filename=".urlencode($filename)); 
    header("Content-Type: application/force-download"); 
    header("Content-Type: application/octet-stream"); 
    header("Content-Type: application/download"); 
    header("Content-Description: File Transfer"); 
    header("Content-Length: " . filesize($file)); 
    flush(); // this doesn't really matter. 

    $fp = fopen($file, "r"); 

    while (!feof($fp)) 
    { 
     echo fread($fp, 65536); 
     flush(); // this is essential for large downloads 
    } 
    fclose($fp); 
} 

$app->close(); 

URL例如: - www.example.com/download.php?file = filename

并确保您已根据需要更改$ link变量。

+0

这只是帮了我很大的忙,谢谢!即使与Joomla 1.5一起工作。不要忘记在.htacess文件中添加拒绝规则。 –