php: get the directory in which resides a file [du

2019-08-31 18:40发布

问题:

Possible Duplicate:
PHP: let a file return its own directory.

Hi

I have a file located in a directory like this:

C:\wamp\www\site\modules\file.php

How can I get the current directory of file.php from within that script? This directory should look like modules in this case

I needs this directory name to build a URL path to a css file from the same directory.

回答1:

dirname(__FILE__) . "/" . basename(__DIR__)

or

dirname(__FILE__)


回答2:

You can take advantage of pathinfo.

$path_parts = pathinfo('C:\wamp\www\site\modules\file.php');

echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // since PHP 5.2.0