programing tip

PHP 현재 디렉토리 이름 가져 오기

itbloger 2020. 8. 3. 08:43
반응형

PHP 현재 디렉토리 이름 가져 오기


내 웹 사이트의 폴더 안에 PHP 페이지가 있습니다.

예를 들어 현재 디렉토리의 이름을 변수에 추가해야합니다.

$myVar = current_directory_name;

이게 가능해?


getcwd();

또는

dirname(__FILE__);

또는 (PHP5)

basename(__DIR__) 

http://php.net/manual/en/function.getcwd.php

http://php.net/manual/en/function.dirname.php

basename()경로의 후미 부분을 얻는 데 사용할 수 있습니다 :)

귀하의 경우에는, 당신이 가장 가능성이 사용을 찾고 있습니다 말하고 싶지만 getcwd(), dirname(__FILE__)당신은 필요가 다른 라이브러리를 포함하고 다른 라이브러리에 포함되어있는 파일이있을 때 더 유용하다.

예 :

main.php
libs/common.php
libs/images/editor.php

에서의 common.php함수를 사용해야 editor.php하므로

common.php:

require_once dirname(__FILE__) . '/images/editor.php';

main.php:

require_once libs/common.php

common.php 인 경우 그 방법 require'dmain.php의 호출 require_oncecommon.php의지 올바르게 포함 editor.phpimages/editor.php대신 위치를 현재 디렉토리에보고 노력의 main.php실행됩니다.


스크립트가 실행 된 디렉토리의 이름 만 가져 오려면 다음을 수행하십시오.

//Path to script: /data/html/cars/index.php
echo basename(dirname(__FILE__)); //"cars"

예를 들어

Your Path = /home/serverID_name/www/your_route_Dir/

THIS_is_the_DIR_I_Want

작동하는 Soultion :

$url = dirname(\__FILE__);
$array = explode('\\\',$url);
$count = count($array);
echo $array[$count-1];

echo basename(__DIR__); will return the current directory name only
echo basename(__FILE__); will return the current file name only

실제로 가장 좋은 해결책은 다음과 같습니다.

$cur_dir = explode('\\', getcwd());
echo $cur_dir[count($cur_dir)-1];

디렉토리가 www \ var \ path \ Current_Path 인 경우

그런 다음 Current_path를 반환합니다.


$myVar = str_replace('/', '', $_SERVER[REQUEST_URI]);

libs / images /index.php
결과 : images

참고 URL : https://stackoverflow.com/questions/9997391/php-get-name-of-current-directory

반응형