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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?