2013-11-01 17 views
5

RecursiveDirectoryIterator dường như để cho tôi hai kết quả khác nhau từ localhost của tôi và máy chủ trực tiếp,php RecursiveDirectoryIterator: làm thế nào để loại trừ đường dẫn thư mục bằng dấu chấm và chấm kép?

define ('WEBSITE_DOCROOT', str_replace('\\', '/', dirname(__FILE__)).'/'); 

print_r(WEBSITE_DOCROOT); 

// List all the class directories in the array. 
$main_directories = array(
    'core/model/', 
    'core/helper/', 
    'core/ext/' 
); 

// Set other vars and arrays. 
$sub_directories = array(); 

// List any sub dirs in the main dirs above and store them in an array. 
foreach($main_directories as $path_directory) 
{ 
    $iterator = new RecursiveIteratorIterator 
    (
     new RecursiveDirectoryIterator(WEBSITE_DOCROOT.$path_directory), // Must use absolute path to get the files when ajax is used. 
     RecursiveIteratorIterator::SELF_FIRST 
    ); 

    foreach ($iterator as $fileObject) 
    { 
     if ($fileObject->isDir()) 
     { 
      //if($fileObject->isDir() === '.' || $fileObject->isDir() === '..') {continue;} 

      // Must trim off the WEBSITE_DOCROOT. 
      $sub_directories[] = preg_replace('~.*?(?=core|local)~i', '', str_replace('\\', '/', $fileObject->getPathname())) .'/'; 
     } 
    } 
} 

// Mearge the main dirs with any sub dirs in them. 
$merged_directories = array_merge($main_directories,$sub_directories); 
print_r($merged_directories); 

localhost,

(
    [0] => core/model/ 
    [1] => core/helper/ 
    [2] => core/ext/ 
) 

máy chủ trực tiếp,

(
    [0] => core/model/ 
    [1] => core/helper/ 
    [2] => core/ext/ 
    [3] => core/model/./ 
    [4] => core/model/../ 
    [5] => core/helper/./ 
    [6] => core/helper/../ 
    [7] => core/ext/./ 
    [8] => core/ext/../ 
) 

Vì vậy, làm thế nào tôi có thể loại trừ đường dẫn thư mục với dấu chấm và chấm kép?

EDIT:

trực máy chủ - PHP phiên bản 5.3.27 Localhost - phiên bản PHP 5,5

Trả lời

21

Hãy thử với:

new RecursiveDirectoryIterator(WEBSITE_DOCROOT.$path_directory, RecursiveDirectoryIterator::SKIP_DOTS); 

Xem http://us1.php.net/manual/en/class.filesystemiterator.php

+1

Tuyệt vời! Tôi đang tìm kiếm điều tương tự, tôi đã loại trừ chúng bằng tay (nếu chấm/dotdot tiếp tục) – Ion

+0

Tôi đã thử nó. nhưng vẫn không hoạt động trên máy chủ trực tiếp của tôi ... – laukok

+0

Sau đó, tôi đoán chúng tôi sẽ cần thêm thông tin về máy chủ của bạn, phiên bản PHP bạn đang chạy, v.v. (cập nhật câu hỏi của bạn) – Brice

Các vấn đề liên quan