2014-01-23 3 views
0

먼저 파일 구조를 설명하겠습니다.
두 레벨 PHP는 현재 디렉토리에서만 작동합니까?

/www/ 
    myfile.php 
    anotherC.php 
    a/ 
     b.php 
     c.php 

myfile.php 내부의 코드는 다음과 같습니다

<?php 
    include_once("a/b.php"); 
?> 

b.php 내부의 코드는 다음과 같습니다 c.php 내부 마침내

<?php 
    include_once("c.php"); 
?> 

그리고 :

<?php 
    echo "hello i'm C.php"; 
?> 
,536

hello i'm C.php

이 작품을 잘 : 나는 www/myfile.php를 호출 할 때

그래서, 내가 출력을 얻을.

Warning: include_once(../anotherC.php): failed to open stream: No such file or directory in /home/hasib/Desktop/www/a/b.php on line 2 Warning: include_once(): Failed opening '../anotherC.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/hasib/Desktop/www/a/b.php on line 2

지금 내 질문은 include_once("c.php");가 완벽하게 근무한다

왜? : 나는 www/myfile.php를 호출 할 때 나 지금
<?php 
    include_once("../anotherC.php"); //or include_once("./c.php"); (it won't work too) 
?> 

b.php을 변경할 수 있습니다, 나는 오류를 얻을

+0

include_path는 무엇입니까? –

+0

내 권장 사항 : 문서 루트 ('$ _SERVER [ 'DOCUMENT_ROOT']')부터 절대 경로를 사용하십시오. 장점은 다음과 같습니다. 실제 코드의 경로를 변경하지 않고도 파일을 이동할 수 있습니다. 중간 파일을 삭제하면 마지막 파일에서 원하는 파일에 계속 액세스 할 수 있습니다. 등등 ... – aleation

+0

@AlexHowansky, 내 포함 경로 :/usr/share/php :/usr/share/pear –

답변

0

상대 경로 포함은 항상 MAIN 스크립트를 기준으로 수행됩니다. include()은 포함 된 데이터를 기본 스크립트에 직접 붙여 넣은 것처럼 동일한 방식으로 작동합니다. 따라서 하위 포함이 수행 될 때 또는 c.php의 작업 디렉토리가 아닌 myFile.php 스크립트의 작업 디렉토리를 사용하고 있습니다.

하위 스크립트의 경우 icnldues에 절대 경로가 있어야하거나 "heck am I"결정 코드 (예 : include(__FILE__ . 'c.php')

+0

이것은 질문에 대답하지 않습니다 : * 왜'include ("c.php")'work *? –

+0

예, 왜'include ("c.php")'? php.net을 살펴본 후 이제 파일을 안전하게 포함시키는 방법을 알고 있습니다. 그러나,'include ("c.php")가 어떻게 작동하지 않는 동안 작동하는지. –

0

내가이 작업에 대해 생각할 수있는 유일한 이유는 사용자가 include_path/www/a을 가지고 있다는 것입니다. 즉, include_once("c.php")은 먼저 /www/c.php을 찾습니다 (현재 작업 디렉토리이므로). 그러면 찾을 수있는 /www/a/c.php을 찾으십시오.

그러나 include_once("./c.php")은 현재 작업 디렉토리에서만 볼 수 있으며 파일이 없으므로 작동하지 않습니다. document

+0

:(필자의 포함 경로는'. :/usr/share/php :/usr/share/pear'입니다. –

1

: 귀하의 예제에서

If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing.

If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or/on Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file.

'호출 스크립트', b.php이다 분명히 그것은 directoy가 '/ www /에서 A /'입니다입니다.

당신이 그렇게

'//www가'반환, 하나 myfile.php 또는 b.php에서 '현재 디렉토리'를 얻을 수에 getcwd()를 사용할 수있을 때 include_once 문 ("c.php") ; 먼저 스크립트 디렉토리를 호출 할 때 c.php를 찾는다. 즉,/www/a /이고, c.php를 성공적으로 얻는다.

include_once ("../ anotherC.php"); , 현재 디렉토리에 상대 경로로 anotherC.php를 찾는다. 현재 디렉토리는/www /이기 때문에 /,/anotherC의 anotherC.php를 찾는다.PHP가 존재하지 않고 경고를 던집니다.

+0

두 번째 경우'include_once ("../ anotherC.php")' 왜 현재 디렉토리가'/ www /'입니까? 전에 말한대로'/ www/a /'가 아니십니까? (그리고 내가 아는 한, 현재 디렉토리는 항상 "/www/"(현재 실행 파일 (myfile.php)이 존재하는 곳). –

+0

@HasibAlMuhaimin은 '현재 디렉토리'와 '호출 스크립트 디렉토리'사이의 차이점에주의하십시오. '현재 디렉토리'를 얻을 수 있습니다. 내가 말했듯이 getcwd()에 의해 결과는 항상/www/유노이다. – leo108

+0

@HasibAlMuhaimin 경로가 ..로 시작할 경우 (../anotherC.php와 같이), php는 ' 현재 디렉토리 ' – leo108

관련 문제