PHP how to go one level up on dirname(__FILE__)
For PHP < 5.3 use: $upOne = realpath(dirname(__FILE__) . ‘/..’); In PHP 5.3 to 5.6 use: $upOne = realpath(__DIR__ . ‘/..’); In PHP >= 7.0 use: $upOne = dirname(__DIR__, 1);
For PHP < 5.3 use: $upOne = realpath(dirname(__FILE__) . ‘/..’); In PHP 5.3 to 5.6 use: $upOne = realpath(__DIR__ . ‘/..’); In PHP >= 7.0 use: $upOne = dirname(__DIR__, 1);
Using Boost.Filesystem: boost::filesystem::path p(“C:\\folder\\foo.txt”); boost::filesystem::path dir = p.parent_path();
You can use basename even though it’s not a file. Strip off the file name using dirname, then use basename to get the last element of the string: dir=”/from/here/to/there.txt” dir=”$(dirname $dir)” # Returns “/from/here/to” dir=”$(basename $dir)” # Returns just “to”
dir=/home/smith/Desktop/Test parentdir=”$(dirname “$dir”)” Works if there is a trailing slash, too.