doujingke4981 2011-11-28 23:48
浏览 156
已采纳

PHP:如何获取当前URL的目录深度?

I'm working on the 404 page for my company's site, and it has a $depth variable that says "../site/". This works fine for any bad urls in the base directory, but anything in a subfolder is grabbing the css files from the wrong place using the $depth variable. I tried $depth = '/site/' and it grabbed no css files at all. My question is, how can I have PHP figure out the $depth dynamically, specifically, the number of ../ to put in? If I can get a number or something, this is easy, but I can't seem to find a quick, easy way to do this.

Edit: Turns out, the 404.php file is only in one location, regardless of which directory the bad url is referencing. So, my real problem is probably not php-related at all. Why would a 404 page get a css file in one folder when reached from www.siteurl.com/404 but get a css file of the same name from a different folder when reached from www.siteurl.com/foo/bar?

  • 写回答

4条回答 默认 最新

  • doulu6234 2011-11-29 03:49
    关注

    Here's a function I wrote a few years ago, I use it on my website. It should help:

    <?
    function absolute_include($file)
             {
             /*
             $file is the file url relative to the root of your site.
             Yourdomain.com/folder/file.inc would be passed as
             "folder/file.inc"
             */
    
             $folder_depth = substr_count($_SERVER["PHP_SELF"] , "/");
    
             if($folder_depth == false)
                $folder_depth = 1;
    
             include(str_repeat("../", $folder_depth - 1) . $file);
             }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等