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'd
에 main.php
의 호출 require_once
에 common.php
의지 올바르게 포함 editor.php
에 images/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
'programing tip' 카테고리의 다른 글
Mercurial에서 특정 버전의 파일을 얻는 방법은 무엇입니까? (0) | 2020.08.03 |
---|---|
JavaScript로 길게 누르시겠습니까? (0) | 2020.08.03 |
그룹 별 총 팬더 비율 (0) | 2020.08.03 |
JPA 2에서 CriteriaQuery를 사용하여 결과를 계산하는 방법 (0) | 2020.08.03 |
div에서 가로 스크롤 막대를 어떻게 제거합니까? (0) | 2020.08.03 |