dongqi0644 2017-06-22 15:32
浏览 58
已采纳

将子文件夹中的文件直接重定向到脚本

I have a subfolder that holds user uploaded files. I want to redirect all direct file requests to this folder to another .php script...so i can check if the user is logged in before i send/show the file to him. For example:

/mainsite/uploads/user/2324/file.pdf

needs to be forwarded to

/mainsite/uploads/permissions.php

But i need inside the permissions.php to be able to do:

$url = $_SERVER['REQUEST_URI'];

and see what was the initial request...in order to readfile() the file after all the 'checking'.

I've tried this:

RewriteEngine On
RewriteRule ^/uploads/user/?$ /uploads/permissions.php [R=301,L]

but this is just a simple forwarding...i got no idea what file or folder the user requested.

I know i can do this by creating an individual htaccess file inside every folder that is created under 'user/{userid}' but i wanted a simpler function. I dont want to have 10000 htaccess files, if i can do this with just one.

Thanks

  • 写回答

1条回答 默认 最新

  • duanhongqiong9460 2017-06-22 15:36
    关注

    try with this syntax: (i added the R=301 part, which wasn't necessary in my version, so it is not fully tested, works without the R option)

    RewriteRule "^/uploads/user/(.+)$" "/uploads/permissions.php?file=$1" [R=301,QSA,L]
    

    You can the get your file var in the $_GET array in permissions.php. However, i wouldn't recommend to use directly this value because it can be unsecure. The best way is to only allow fixed values, with a switch for example, having filtered the var as a string before.

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

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!