dongnvwang8591 2013-05-14 16:35
浏览 61
已采纳

.htaccess限制wordpress中的媒体文件夹

I am trying to figure out how to use this in a wordpress environment:

From: Block direct url access but allow download

1st: .htaccess(in uploads folder)

Order Deny,Allow
Deny from all

2nd: (in wp-content folder "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;
    }
  //}
}

If I comment out the is_user_logged_in statement it works like a charm. I would like to add some conditional statements so I can serve the file to a role or even specific users by id or name. I can to the leg work but I am not sure how to get the needed functions from WordPress.

in the src area I put (wp-content directory)/thePHPfile.php?name=my-image-name

How do I use wordpress functions in the file?

  • 写回答

1条回答 默认 最新

  • dongtu1958 2013-05-14 16:42
    关注

    In order to call Wordpress functions from an arbitrary script you'll need to include the Wordpress "start". This is not index.php. The file you need to include is wp-blog-header.php from your wordpress root directory. Once this is loaded, it will give you access to all the Wordpress functions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题