2013-05-14 59 views
0

我試圖找出如何在WordPress的環境中使用此媒體文件夾:的.htaccess限制在WordPress

來源:Block direct url access but allow download

1: 的.htaccess(在上傳文件夾)

Order Deny,Allow 
Deny from all 

第二:(在可溼性粉劑內容文件夾 「thePHPfile.php」)

if(!empty($_GET['name'])) 
{ 

//if(is_user_logged_in()) 
    //{ 
    $file_name = preg_replace('#[^-\w]#', '', $_GET['name']); 
    $the_file = "{$_SERVER['DOCUMENT_ROOT']}/wp-content/uploads/2013/05/oprotunity.jpg"; 
    //$the_file = "{$_SERVER['DOCUMENT_ROOT']}/wp-content/uploads/2013/05/{$file_name}.jpg"; 
    if(file_exists($the_file)) 
    { 
     header('Cache-Control: public'); 
     header('Content-Description: File Transfer'); 
     header("Content-Disposition: attachment; filename={$the_file}"); 
     header('Content-Type: image/jpeg'); 
     header('Content-Transfer-Encoding: binary'); 
     readfile($the_file); 

     exit; 
    } 
    //} 
} 

如果我將is_user_logged_in聲明註釋掉,它就像魅力一樣。我想添加一些條件語句,以便我可以通過id或名稱將文件提供給角色甚至特定用戶。我可以去腿部工作,但我不知道如何從WordPress獲得所需的功能。

在src區我把(WP-內容目錄)

/thePHPfile.php?name=my-image-name

如何在文件中使用WordPress的功能呢?

回答

1

爲了從任意腳本調用Wordpress函數,您需要包含Wordpress「開始」。這是而不是 index.php。您需要包含的文件是您的wordpress根目錄中的wp-blog-header.php。一旦這個被加載,它會讓你訪問所有的WordPress的功能。

+0

<?php require('/ the/path/to/your/wp-blog-header.php'); ?> – Ernest 2013-05-14 16:54:09

+0

在你看來,這是一個很好的方法來濾除用戶看到某些媒體文件? – Ernest 2013-05-14 16:55:28

+0

@ Ernest:它只與認證措施本身一樣安全。原則上,這是一個很好的方法。 – 2013-05-14 16:56:49