阻止访客用户直接访问PDF链接

时间:2014-03-31 20:45:24

标签: .htaccess security pdf joomla joomla2.5

我有一个joomla网站。并将一些pdf文件放入网站的根目录。

有没有办法保护DIRECT ACCESS到GUESTS(公共)用户的pdf,并允许注册用户?

我尝试使用htaccess(deny),但注册用户也无法直接查看pdf。 搜索但没有发现任何相关内容。请有人帮忙。

谢谢

3 个答案:

答案 0 :(得分:2)

如果您不想编写自己的php代码,则必须使用文档管理插件.SO,DOCman是Joomla强大的文档管理解决方案。您可以通过以下链接进行检查。

http://extensions.joomla.org/extensions/directory-a-documentation/downloads/10958

答案 1 :(得分: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();

网址示例: - www.example.com/download.php?file=filename

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

答案 2 :(得分:0)

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

拒绝所有

它应位于pdf文件所在的发票文件夹下。

如果您使用deny from all then文件,则没有htacess文件所在的特定目录的下载访问权限。

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

网址示例: - www.example.com/index.php?option=com_temp&task=temp.downloadmypdf&file=filename

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。

相关问题