Get parent folder of current file PHP

后端 未结 5 1473
借酒劲吻你
借酒劲吻你 2020-12-29 05:09

I\'ve done quite a bit of searching for this, so I\'m sorry if this is a dupe.
Anyway, I need to get the name of the folder the current file is in. For example, I want

相关标签:
5条回答
  • 2020-12-29 05:34
    str_replace('/','',$_SERVER[REQUEST_URI]);
    

    This works for php7: str_replace('/','',$_SERVER["REQUEST_URI"]);

    Result:

    lib/**images**/index.php

    images

    0 讨论(0)
  • 2020-12-29 05:36
    $folder = basename(dirname(__FILE__));
    
    0 讨论(0)
  • 2020-12-29 05:40

    You need to combine your existing code using dirname() with a call to basename():

    $parent = basename(dirname($_SERVER['PHP_SELF']));
    
    0 讨论(0)
  • 2020-12-29 05:40

    dirname() used with basename() would work ... also this if you want to get them all:

    $folders = explode ('/', $_SERVER['PHP_SELF']);
    

    Now $folders would contain an array of all of the folder names.

    Cheers.

    0 讨论(0)
  • 2020-12-29 05:50

    The simpliest way is:

    basename(__DIR__);
    

    http://php.net/manual/en/language.constants.predefined.php

    0 讨论(0)
提交回复
热议问题