php: get the directory in which resides a file [duplicate]

喜夏-厌秋 提交于 2019-12-13 21:17:18

问题


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


来源:https://stackoverflow.com/questions/5804203/php-get-the-directory-in-which-resides-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!