PHP访问文件目录进行权限验证

福尔摩斯对我笑 2019-02-18 13199 次 1460条
应领导要求,对现有的文件访问进行权限控制,必须登录后才能直接访问文件。 之前做过PHP+Nginx的版本,当时也算勉强实现了。 本来打算利用cookie和apache的配置文件进行权限控制,不过,由于Ecshop登录后并没有cookie,无奈只能放弃。 也考虑过利用Apache判断session,查了很多资料,也没找到解决办法。 今天上了个洗手间,在放飞自我的同时,也放飞了下大脑,想到了解决办法。 其实很简单:查询文件时-》如果存在这个文件-》复制一份到临时目录-》将临时目录中的文件返给前端-》查询时先清空该目录下的文件。

关键PHP代码

//读取该文件
$fp=fopen($file,'r');
$buffer='';
while(!feof($fp))
{
  $buffer.=fgets($fp); //读取二进制流
}
fclose($fp);
//存储新的文件  生成唯一的文件名
$path = "./temp/coa/".uniqid(mt_rand(),1)."_".$coaFileName.".pdf";
$ret  = file_put_contents($path, $buffer);
			
if ($ret) {
  ajax_json('',0,$path, $coaFileName);
}else{
  ajax_json('The read failed, please contact the administrator.');
}

利用.htaccess文件对目录进行访问控制

Order allow,deny
Deny from all
 将该文件放到你要屏蔽的文件目录下,即可。
最后看下,其实也不难,当然我这种方法并不太好。不过这是我目前所能想到的啦。
标签: /

非特殊说明,本博所有文章均为博主原创。

头像也是LOGO
未来博客 博主大人
到了这个年纪,不应该再彷徨!

发表评论



 


已有 1460 条评论 1


  1. yeezy shoes

    I am also commenting to make you understand what a cool discovery my friend’s princess undergone going through your web site. She figured out so many things, which included what it’s like to have an amazing giving spirit to have other folks without problems gain knowledge of several hard to do things. You undoubtedly surpassed her expected results. Thank you for distributing such precious, dependable, explanatory as well as fun tips about the topic to Emily.

    回复 2019年10月17日
  2. lebron shoes

    Thank you so much for giving everyone an exceptionally pleasant opportunity to read from here. It’s always very great and full of a good time for me and my office fellow workers to visit your website a minimum of 3 times a week to find out the fresh guides you will have. And lastly, we’re actually amazed with the magnificent suggestions you give. Some two points in this posting are particularly the simplest I’ve had.

    回复 2019年10月16日
  3. fila

    I would like to express my admiration for your generosity giving support to visitors who really need help with this one subject. Your personal dedication to getting the message all around became astonishingly important and have usually encouraged many people just like me to get to their pursuits. Your own important information signifies much to me and extremely more to my peers. Thanks a lot; from everyone of us.

    回复 2019年10月14日
  4. coach factory outlet

    Thanks so much for giving everyone such a superb opportunity to read in detail from this site. It’s always very ideal plus jam-packed with fun for me personally and my office friends to visit your website really 3 times weekly to read the latest issues you have. Of course, I’m also always amazed with the surprising points served by you. Some 2 areas in this posting are ultimately the simplest we have had.

    回复 2019年10月13日
  5. moncler

    I have to get across my gratitude for your kindness in support of women who have the need for assistance with this important theme. Your very own dedication to getting the message around appears to be extraordinarily productive and have continuously made employees like me to achieve their objectives. The insightful advice implies so much to me and a whole lot more to my office workers. Many thanks; from all of us.

    回复 2019年10月12日
  6. balenciaga shoes

    I wish to convey my love for your kind-heartedness for all those that really want help with this concept. Your very own dedication to getting the solution throughout came to be amazingly important and has continually allowed others much like me to achieve their dreams. Your amazing valuable guidelines means a whole lot a person like me and substantially more to my office colleagues. Regards; from everyone of us.

    回复 2019年10月11日
  7. moncler jackets

    I would like to express my appreciation to you for rescuing me from such a setting. After searching through the world wide web and finding things that were not pleasant, I figured my life was well over. Being alive without the solutions to the problems you’ve fixed as a result of your main posting is a serious case, as well as ones which may have in a wrong way affected my entire career if I had not noticed your web page. Your actual expertise and kindness in handling all the details was useful. I’m not sure what I would have done if I had not come upon such a point like this. I’m able to at this point look forward to my future. Thanks very much for your impressive and results-oriented help. I won’t think twice to suggest your web sites to anybody who should receive counselling on this issue.

    回复 2019年10月11日
  8. golden goose

    Thanks a lot for giving everyone an extremely memorable chance to read articles and blog posts from this blog. It is usually so pleasant plus jam-packed with a great time for me personally and my office colleagues to visit your website the equivalent of three times in a week to read through the newest guidance you have got. Not to mention, I’m always happy concerning the magnificent methods served by you. Some 2 points on this page are in reality the very best I have ever had.

    回复 2019年10月10日
  9. kyrie 5 shoes

    I needed to compose you the very small word so as to thank you so much as before for the spectacular pointers you’ve provided above. This is particularly generous of people like you to present extensively precisely what many people would have marketed as an ebook to help with making some profit for themselves, most notably considering the fact that you might have tried it in the event you wanted. The concepts likewise acted like a great way to comprehend many people have similar keenness really like mine to know great deal more with regard to this matter. I’m certain there are thousands of more enjoyable times up front for those who scan your blog post.

    回复 2019年10月9日
  10. birkin bag

    I precisely wanted to appreciate you once more. I’m not certain what I would have gone through in the absence of the entire information discussed by you concerning this concern. It became the depressing issue in my circumstances, however , being able to see a professional form you dealt with it took me to weep over joy. Extremely grateful for the guidance and then sincerely hope you find out what a powerful job that you’re doing teaching men and women using your webpage. Most probably you’ve never got to know any of us.

    回复 2019年10月8日