2012-01-12 4 views
0

질문이 있습니다. 아마도 간단한 질문 일 것입니다. 내가하고 싶은 것은 PHP 코드 영역의 폴더에있는 각 .php 파일의 맨 위에 고유 한 이름을 붙일 수 있어야한다는 것입니다. 그런 다음 index.php 파일의 스크립트를보고 그 폴더를보고 PHP 코드의 각 페이지 상단에있는 각 .php 파일의 고유 한 이름을 추출하여 색인의 목록에 표시합니다. PHP 페이지.페이지 상단에 .php 파일의 고유 이름을 지정하고 index.php 파일에 고유 한 이름을 표시하는 방법

나는 페이지 상단에이 같이해야 할 것이다 :

< ? 
{{{{{uniquenamehere}}}}} 
? > 

그리고

그렇다면, 어떤 것 uniquenamehere를 잡아와의 index.php 페이지에 표시 같이 코드를 작성하지 않았나요?

미리 감사드립니다. 내 질문에 더 명확해야 할 필요가 있다면 알려주십시오. 정말 간단한 질문이라면 미안 해요. 경고 : 아래의 답변을 사용하는 경우

편집

이 경고를 얻기 file_get_contents (test.php를) [function.file-get은 - 내용] : /에/경로 그런 파일이나 디렉토리 : 스트림을 열지 못했다 index.php를

여기 내가 사용하고있는 코드,

<?php 

// Scan directory for files 
$dir = "path/"; 
$files = scandir($dir); 

// Iterate through the list of files 
foreach($files as $file) 
{ 
// Determine info about the file 
$parts = pathinfo($file); 

// If the file extension == php 
if ($parts['extension'] === "php") 
{ 
// Read the contents of the file 
$contents = file_get_contents($file); 

// Find first occurrence of opening template tag 
$from = strpos($contents, "{{{{{"); 

// Find first occurrence of ending template tag 
$to = strpos($contents,"}}}}}"); 

// Pull out the unique name from between the template tags 
$uniqueName = substr($contents, $from+5, $to); 

// Print out the unique name 
echo $uniqueName ."<br/>"; 
} 
} 
?> 
+1

파일 이름을 사용할 수 없습니까? 당신이 원하는 것은 가능하지만 지저분한 접근법처럼 보입니다. – DaveRandom

+0

"고유 한 이름"또는 몇 가지 예를 들어주세요. * 편집 : * MySQL과 관련하여 무엇이 필요합니까? – FakeRainBrigand

+0

@DaveRandom에 동의합니다. 파일 이름 만 사용하십시오. 각 파일을 열고 읽는 것보다 훨씬 쉬울 것입니다. –

답변

1

테스트되지 않음, 그러나 그것은이 같은 대략 뭔가해야합니다.

<?php 

// Scan directory for files 
$fileInfo = pathinfo(__FILE__); 

$dir = $fileInfo['dirname']; 
$files = scandir($dir); 

// Iterate through the list of files 
foreach($files as file) 
{ 
    // Determine info about the file 
    $parts = pathinfo($file); 

    // If the file extension == php 
    if ($parts['extension'] == "php") 
    { 
    // Read the contents of the file 
    $contents = file_get_contents($file); 

    // Find first occurrenceof opening template tag 
    $from = strpos($contents, "{{{{{"); 

    // Find first occurrenceof ending template tag 
    $to = strpos($contents,"}}}}}"); 

    // Pull out the unique name from between the template tags 
    $uniqueName = substr($contents, $from+5, $to); 

    // Print out the unique name 
    echo $uniqueName ."<br/>"; 
    } 
} 
+0

디렉토리에서 파일을 찾지 만 {{{{{}}}} 경고 : file_get_contents (test.php) [function.file-get-contents] : 이름을 가져 오지 못했습니다. open stream : /path/index.php에 같은 파일이나 디렉토리가 없습니다. – KGDD

+0

'$ dir' 경로가 index.php와 같은 위치에없는 것 같습니다. 임의의 디렉토리가 아닌 스크립트의 현재 디렉토리를 검색하도록 위의 스크립트를 수정했습니다. –

관련 문제