차근차근/PHP

디렉토리 함수 목록

예쁜꽃이피었으면 2014. 7. 30. 10:23

chdir — 디렉토리 변경

bool chdir ( string $directory )

http://www.php.net/manual/kr/function.chdir.php

 

 

chroot

bool chroot ( string $directory )

 

http://www.php.net/manual/kr/function.chroot.php

dir

Directory {
string $Directory->path ;
resource $handle ;
string Directory::read ( void )
void Directory::rewind ( void )
void Directory::close ( void )

}

http://www.php.net/manual/kr/class.dir.php

 

 

 

closedir

void closedir ([ resource $dir_handle ] )

 

http://www.php.net/manual/kr/function.closedir.php

설명 ¶

void closedir ([ resource $dir_handle ] )

dir_handle가 지시하는 디렉토리 스트림을 닫습니다. 스트림은 opendir()로 열려 있는 것이여야 합니다.

 

getcwd

string getcwd ( void )

http://www.php.net/manual/kr/function.getcwd.php

 

 

 

 

opendir

resource opendir ( string $path [, resource $context ] )

http://www.php.net/manual/kr/function.opendir.php

 

설명 ¶

resource opendir ( string $path [, resource $context ] )

closedir()readdir()rewinddir() 호출에서 사용하는 디렉토리 핸들을 엽니다.

인수 ¶

path

열 디렉토리 경로

context

context 인수에 대한 설명은 매뉴얼의 스트림 섹션을 참고하십시오.

반환값 ¶

성공시엔 디렉토리 핸들 resource, 실패시엔 FALSE를 반환합니다.

path가 유효하지 않은 디렉토리이거나, 권한 제한이나 파일시스템 오류로 인해 디렉토리를 열 수 없는 경우, opendir()은 FALSE를 반환하고 E_WARNING 레벨의 PHP 오류를 발생합니다. opendir() 앞에 '@'를 붙여 오류 출력을 없앨 수 있습니다.

변경점 ¶

버전 설명
5.0.0 path가 ftp:// URL 래퍼를 지원합니다.
4.3.0 path에 디렉토리 목록을 지원하는 URL을 사용할 수 있습니다. 그러나 PHP 4에서는 file://URL 래퍼만 지원합니다.

예제 ¶

Example #1 opendir() 예제

<?php
$dir 
"/etc/php5";

// 알고 있는 디렉토리를 열어서, 내용을 읽어들이는 작업입니다.
if (is_dir($dir)) {
    if (
$dh opendir($dir)) {
        while ((
$file readdir($dh)) !== false) {
            echo 
"filename: $file : filetype: " filetype($dir $file) . "\n";
        }
        
closedir($dh);
    }
}
?>

위 예제의 출력 예시:

filename: . : filetype: dir
filename: .. : filetype: dir
filename: apache : filetype: dir
filename: cgi : filetype: dir
filename: cli : filetype: dir

 

 

readdir

string readdir ([ resource $dir_handle ] )

http://www.php.net/manual/kr/function.readdir.php

 

readdir

(PHP 4, PHP 5)

readdir — 디렉토리 핸들에서 엔트리를 읽습니다

설명 ¶

string readdir ([ resource $dir_handle ] )

디렉토리에서 다음 파일의 파일명을 반환합니다. 반환하는 파일명의 순서는 파일시스템에 저장한 순서입니다.

인수 ¶

dir_handle

opendir()로 열린 디렉토리 핸들 resource. 디렉토리 핸들이 지정되지 않으면, opendir()로 열린 마지막 연결을 할당합니다.

반환값 ¶

성공시에는 파일명, 실패시에는 FALSE를 반환합니다.

Warning

이 함수는 논리 FALSE를 반환하지만, 0이나 "" 등, 논리 FALSE로 취급할 수 있는 다른 값을 반환할 수 있습니다. 자세한 정보는 논리형 섹션을 참고하십시오. 이 함수의 반환값을 확인하려면 === 연산자를 이용하십시오.

예제 ¶

Example #1 디렉토리 안의 모든 파일 목록

readdir()의 반환값은 아래 예제체럼 확인해야 하는 점에 주의하십시오. 반환값이 FALSE와 완전히 동일한지 (형까지 일치--자세한 정보는 비교 연산자를 참고하십시오) 확인하고 있습니다. 이렇게 하지 않으면, FALSE로 인식하는 디렉토리 엔트리에서 루프가 멈춥니다. (예. 디렉토리 이름이 "0")

<?php
// !== 은 4.0.0-RC2까지 존재하지 않았던 점에 주의하십시오.

if ($handle opendir('/path/to/files')) {
    echo 
"Directory handle: $handle\n";
    echo 
"Files:\n";

    
/* 디렉토리 안을 루프하는 올바른 방법입니다. */
    
while (false !== ($file readdir($handle))) {
        echo 
"$file\n";
    }

    
/* 디렉토리 안을 루프하는 *잘못된* 방법입니다. */
    
while ($file readdir($handle)) {
        echo 
"$file\n";
    }

    
closedir($handle); 
}
?>

Example #2 .과 ..을 제외한 현재 디렉토리의 모든 파일 목록

<?php
if ($handle opendir('.')) {
    while (
false !== ($file readdir($handle))) {
        if (
$file != "." && $file != "..") {
            echo 
"$file\n";
        }
    }
    
closedir($handle);
}
?>

참고 ¶

  • is_dir() - Tells whether the filename is a directory
  • glob() - Find pathnames matching a pattern
  • opendir() - 디렉토리 핸들을 엽니다
  • scandir() - 지정한 경로 안의 파일과 디렉토리 목록

 add a note

User Contributed Notes 27 notes

 
ekumar [at] sreek [dot] in ¶
3 years ago

/** 
* Finds path, relative to the given root folder, of all files and directories in the given directory and its sub-directories non recursively. 
* Will return an array of the form 
* array( 
*   'files' => [], 
*   'dirs'  => [], 
* ) 
* @author sreekumar 
* @param string $root 
* @result array 
*/ 
function read_all_files($root '.'){ 
  
$files  = array('files'=>array(), 'dirs'=>array()); 
  
$directories  = array(); 
  
$last_letter  $root[strlen($root)-1]; 
  
$root  = ($last_letter == '\\' || $last_letter == '/') ? $root :$root.DIRECTORY_SEPARATOR
  
  
$directories[]  = $root
  
  while (
sizeof($directories)) { 
    
$dir  array_pop($directories); 
    if (
$handle opendir($dir)) { 
      while (
false !== ($file readdir($handle))) { 
        if (
$file == '.' || $file == '..') { 
          continue; 
        } 
        
$file  $dir.$file
        if (
is_dir($file)) { 
          
$directory_path $file.DIRECTORY_SEPARATOR
          
array_push($directories$dire





4
frasq at frasq dot org ¶
3 years ago
A variation on listing all the files in a directory recursively. The code illustrates a basic technique : the use of an auxiliary function. It avoids building temporary lists which are merged on the way back. Note that the array which collects the information must be passed by reference. 

<?php 
function listdir($dir='.') { 
    if (!
is_dir($dir)) { 
        return 
false
    } 
    
    
$files = array(); 
    
listdiraux($dir$files); 

    return 
$files


function 
listdiraux($dir, &$files) { 
    
$handle opendir($dir); 
    while ((
$file readdir($handle)) !== false) { 
        if (
$file == '.' || $file == '..') { 
            continue; 
        } 
        
$filepath $dir == '.' $file $dir '/' $file
        if (
is_link($filepath)) 
            continue; 
        if (
is_file($filepath)) 
            
$files[] = $filepath
        else if (
is_dir($filepath)) 
            
listdiraux($filepath$files); 
    } 
    
closedir($handle); 


$files listdir('.'); 
sort($filesSORT_LOCALE_STRING); 

foreach (
$files as $f) { 
    echo  
$f"\n"

?>
6
stuart at horuskol dot net ¶
3 years ago
Looking through the examples, I can't see any that do a simple check on the value of the directory resource that opendir returns and is subsequently used by readdir.

If opendir returns false, and you simply pass this to the readdir call in the while loop, you will get an infinite loop. 

A simple test helps prevent this:

<?php

$dir 
opendir($path);
while (
$dir && ($file readdir($dir)) !== false) {
  
// do stuff
}

?>
2
hanan dot ali dot shaikh at gmail dot com ¶
5 years ago
This function is used to display random image i.e. at header position of a site. It reads the whole directory and then randomly print the image. I think it may be useful for someone. 

<?php 
if ($handle opendir('images/')) { 
   
$dir_array = array(); 
    while (
false !== ($file readdir($handle))) { 
        if(
$file!="." && $file!=".."){ 
            
$dir_array[] = $file
        } 
    } 
    echo 
$dir_array[rand(0,count($dir_array)-1)]; 
    
closedir($handle); 

?>
4
DaveRandom ¶
5 years ago
A very flexible function to recursively list all files in a directory with the option to perform a custom set of actions on those files and/or include extra information about them in the returned data.

----------

SYNTAX:
   $array = process_dir ( $dir , $recursive = FALSE )
        $dir  (STRING)   =  Directory to process
  $recursive  (BOOLEAN)  =  [Optional] Recursive if set to TRUE

RETURN VALUES:
  The function returns an indexed array, one entry for every file. Each entry is an associative array, containing the basic information 'filename' (name of file) and 'dirpath' (directory component of path to file), and any additional keys you configure. Returns FALSE on failure.

----------

  To allow you to configure another key, the entry for each file is stored in an array, "$entry" for each iteration. You can easily return any additional data for a given file using $entry['keyname'] = ... (Note that this data can be any variable type - string, bool, float, resource etc)

  There is a string variable "$path" available, which contains the full path of the current file, relative to the initial "$dir" supplied at function call. This data is also available in it's constituent parts, "$dir" and "$file". Actions for each file can be constructed on the basis of these variables. The variables "$list", "$handle" and "$recursive" should not be used within your code.

----------

Simply insert you code into the sections indicated by the comments below and your away!

The following example returns filename, filepath, and file modified time (in a human-readable string) for all items, filesize for all files but not directories, and a resource stream for all files with 'log' in the filename (but not *.log files).

<?php

  
function process_dir($dir,$recursive FALSE) {
    if (
is_dir($dir)) {
      for (
$list = array(),$handle opendir($dir); (FALSE !== ($file =readdir($handle)));) {
        if ((
$file != '.' && $file != '..') && (file_exists($path $dir.'/'.$file))) {
          if (
is_dir($path) && ($recursive)) {
            
$list array_merge($listprocess_dir($pathTRUE));
          } else {
            
$entry = array('filename' => $file'dirpath' => $dir);

//---------------------------------------------------------//
//                     - SECTION 1 -                       //
//          Actions to be performed on ALL ITEMS           //
//-----------------    Begin Editable    ------------------//

  
$entry['modtime'] = filemtime($path);

//-----------------     End Editable     ------------------//
            
do if (!is_dir($path)) {
//---------------------------------------------------------//
//                     - SECTION 2 -                       //
//         Actions to be performed on FILES ONLY           //
//-----------------    Begin Editable    ------------------//

  
$entry['size'] = filesize($path);
  if (
strstr(pathinfo($path,PATHINFO_BASENAME),'log')) {
    if (!
$entry['handle'] = fopen($path,r)) $entry['handle'] = "FAIL";
  }
  
//-----------------     End Editable     ------------------//
              
break;
            } else {
//---------------------------------------------------------//
//                     - SECTION 3 -                       //
//       Actions to be performed on DIRECTORIES ONLY       //
//-----------------    Begin Editable    ------------------//

//-----------------     End Editable     ------------------//
              
break;
            } while (
FALSE);
            
$list[] = $entry;
          }
        }
      }
      
closedir($handle);
      return 
$list;
    } else return 
FALSE;
  }
    
  
$result process_dir('C:/webserver/Apache2/httpdocs/processdir',TRUE);

// Output each opened file and then close
  
foreach ($result as $file) {
    if (
is_resource($file['handle'])) {
        echo 
"\n\nFILE (" $file['dirpath'].'/'.$file['filename'] . "):\n\n" .fread($file['handle'], filesize($file['dirpath'].'/'.$file['filename']));
        
fclose($file['handle']);
    }
  }

?>
1
Blizzz ¶
10 months ago
If dir_handle is not a proper resource, null will be returned instead of false.
2
Mauro Alessandro Nonnis ¶
2 years ago
## List and Rename  all files on recursive directories with "recursive directory name" as template + filename
## Advice:  other files in the same directory will result in a warning 
## scriptname : Recursive Dir_Renfiles_dirname-filename.php

<?php
if ($handle opendir('.')) {
    while (
false !== ($file readdir($handle))) {
                if (
$file != "." && $file != ".." && $file != "Recursive Dir_Renfiles_dirname-filename.php") {
                    echo 
"$file";
        echo 
"<br>";
                    
$count = -1;
                     
$handle2 = @opendir($file);
                        while (
false !== ($file2 = @readdir($handle2))) {
                        echo 
"$file2";      
                        if (
$count <10 ){ @rename("$file/$file2","$file/$file"."_$file2");}
                        else { @
rename("$file/$file2""$file/$file"."_$file2");}
                        echo 
"<br>";
                         
$count $count 1;
                            }
             echo 
"<br>";
        }
    }
    
closedir($handle);
}
?>
2
avi dot megladon at gmail dot com ¶
2 years ago
loop through folders and sub folders with option to remove specific files. 

<?php 
function listFolderFiles($dir,$exclude){ 
    
$ffs scandir($dir); 
    echo 
'<ul class="ulli">'
    foreach(
$ffs as $ff){ 
        if(
is_array($exclude) and !in_array($ff,$exclude)){ 
            if(
$ff != '.' && $ff != '..'){ 
            if(!
is_dir($dir.'/'.$ff)){ 
            echo 
'<li><a href="edit_page.php?path='.ltrim($dir.'/'.$ff,'./').'">'.$ff.'</a>'
            } else { 
            echo 
'<li>'.$ff;    
            } 
            if(
is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff,$exclude); 
            echo 
'</li>'
            } 
        } 
    } 
    echo 
'</ul>'


listFolderFiles('.',array('index.php','edit_page.php')); 
?>
1
hotrungdungit at gmail dot com ¶
5 years ago
Below will return an array of file names and folders in directory 

<?php 
function ReadFolderDirectory($dir "root_dir/here"
    { 
        
$listDir = array(); 
        if(
$handler opendir($dir)) { 
            while ((
$sub readdir($handler)) !== FALSE) { 
                if (
$sub != "." && $sub != ".." && $sub != "Thumb.db") { 
                    if(
is_file($dir."/".$sub)) { 
                        
$listDir[] = $sub
                    }elseif(
is_dir($dir."/".$sub)){ 
                        
$listDir[$sub] = $this->ReadFolderDirectory($dir."/".$sub); 
                    } 
                } 
            }    
            
closedir($handler); 
        } 
        return 
$listDir;    
    } 
?>
1
Qwerp ¶
3 years ago
Here's a handy function you can use to list the files in the directory you specify, their type (dir or file) and whether they are hidden. 
You could modify it to do other things too.

<?php
function listDirs($where){
echo 
"<table border=\"1\"><tr><td><b>Name</b></td><td><b>Type</b></td>";
echo 
"<td><b>Invisible (Hidden)?</b></td></tr>";
    
$itemHandler=opendir($where);
    
$i=0;
    while((
$item=readdir($itemHandler)) !== false){
        if(
substr($item01)!="."){
            if(
is_dir($item)){
                echo 
"<tr><td>$item</td><td>Directory</td><td>No</td></tr>";
            }else{
                echo 
"<tr><td>$item</td><td>File</td><td>No</td></tr>";
            }
            
$i++;
        }else{
            if(
is_dir($item)){
                echo 
"<tr><td>$item</td><td>Directory</td><td>Yes</td></tr>";
            }else{
                echo 
"<tr><td>$item</td><td>File</td><td>Yes</td></tr>";
            }
            
$i++;
        }
       }
echo 
"</table>";
}
?>
Then call it like this.
<?php
listDirs
(DIR);
?>
Example:
<?php
listDirs
("/tests/directorylisting");
?>

You get a table like this.

Name    Type    Invisible (Hidden)?
.    Directory    Yes
..    Directory    Yes
.DS_Store    File    Yes
.localized    File    Yes
index.php    File    No
OOOLS    Directory    No
QwerpWiki.php    File    No
2
HeadRoom ¶
5 years ago
Thought I would include what I wrote to get a random image from a directory. 

<?php 
$image_dir 
'images'
$count 0
if (
$handle opendir($image_dir)) { 
    
$retval = array(); 
    while (
false !== ($file readdir($handle))) { 
        if ((
$file <> ".") && ($file <> "..")) { 
        
$retval[$count] = $file
        
$count $count 1
            } 
    } 

    
closedir($handle); 

shuffle($retval); 
$current_image $retval[0]; 
?> 

[NOTE BY danbrown AT php DOT net: Contains a bugfix/typofix inspired by 'ffd8' on 19-JUN-09.]
2
nullbyte at hotmail dot com ¶
6 years ago
I haven't tested this yet, but it seems like it'll do just fine if you need files of a certain extension: 

<?php 
$dh 
opendir($options['inputDir']); 
$files = array(); 
while ((
$filename readdir($dh)) !== false

    if (
substr($filenamestrrpos($filename'.')) == $options['inputExt']) 
    { 
        
$files[] = $filename
    } 

closedir($dh); 
?>
1
jpittman2 at gmail dot com ¶
11 months ago
Warning when using readdir() on certain versions of CentOS on NFS-mounted directories:

This is not a bug with PHP's readdir, but a bug with certain versions of CentOS's readdir implementation.  According to Post #6213 in the CentOS Bugs forum, when using CentOS kernel versions 2.6.18-348 through 2.6.18-348.3.1, invoking readdir on an NFS-mounted directory may not return all the entries.  Since PHP's readdir() uses this library, the issue is manifest in PHP as well.

According to the post, upgrading to version 2.6.18-348.4.1.el5 should solve the issue, though I haven't tried it.

glob() does NOT seem to suffer from this same vulnerability.
1
gherson at snet dot net ¶
4 years ago
readdir_recursive() is a recursive readdir() that returns filespecs not filenames. 

<?php 

define
('SEEK''the grail'); 
define('ORIGIN'"C:\\Documents and Settings\\myname\\My Documents"); 
$hits = array(); 
$handles = array(ORIGIN=>opendir(ORIGIN)); // Associate handle with its (absolute) path. 
////\\\\ "$handles" is special to 
//                 \\\\\\\\\//////// 
while ($filespec readdir_recursive($handles)) { 
    
$out = array(); 
    
$cmd "c:\\cygwin\\bin\\strings '$filespec' | c:\\cygwin\\bin\\grep -li ".SEEK
    
$lastLine exec($cmd$out$status); 
    
// echo "out is ". implode(' ', $out) .", lastLine is $lastLine, status is $status\n"; 

    
if (strstr(' 'implode(' '$out), '(standard input)')) { // Hit. 
       
$hits[] = $filespec
    } 


echo 
"'".SEEK."' found in files: <br/>\n"implode("<br/>\n"$hits); 

/** 
* Return unique filespecs under given directory tree until all found. 

* To use: while ($filespec = readdir_recursive(array($dir="\\path\\to\\folder" => opendir($dir)))) { ... 

* @param mixed $seed Array(<path>=><resource>) on 1st call | false during recursion. 
* @return mixed $filespec String | false 
*/ 
function readdir_recursive($seed false

    global 
$handles// ("global" instead of "static" because with latter, 
    // assignments go out of scope when function executions end (via return $filespec;).) 
    
    // $seed is only true (ie, includes a directory path specified by the user) 
    // when readdir_recursive() is executing for the 1st time. 
    
if ($seed) { 
        
$handles $seed
    } 
    
assert('is_array($handles)'); 

    if (empty(
$handles)) { 
        return 
false// All done. 
    


    
// Advance the current($handles) handle to a new file or folder. 
    
$file readdir(current($handles)); 
    
$filespec key($handles) ."\\"$file
    if (
false===$file) { // End of current($handles) directory reached. 

        // The current element, an exhausted handle, is the last element. 
        
array_pop($handles); // Discard exhausted handle. (This also sets array pointer to array beginning.) 
        
end($handles); 
        
// echo "\nUp to element ". key($handles) .'=>'. current($handles) ."\n"; 

        
return readdir_recursive(); // Resume finding files in a higher level directory. 

    
} elseif (is_file($filespec)) { 
        return 
$filespec

    } else { 
// $file is a directory: Recurse. 
        
if ($file!=='.' && $file!=='..') { // Non-pointer directory reached. 
            // Add this subdirectory to the list that must be traversed. 
            
$handles[$filespec] = opendir($filespec); 
            
next($handles); // Prepare to proceed to the appended subdirectory. 
        

        return 
readdir_recursive(); 
    } 
}
1
schursin at gmail[deleteme] dot com ¶
7 years ago
code:

<?php

        
function permission($filename)
        {
            
$perms fileperms($filename);

            if     ((
$perms 0xC000) == 0xC000) { $info 's'; }
            elseif ((
$perms 0xA000) == 0xA000) { $info 'l'; }
            elseif ((
$perms 0x8000) == 0x8000) { $info '-'; }
            elseif ((
$perms 0x6000) == 0x6000) { $info 'b'; }
            elseif ((
$perms 0x4000) == 0x4000) { $info 'd'; }
            elseif ((
$perms 0x2000) == 0x2000) { $info 'c'; }
            elseif ((
$perms 0x1000) == 0x1000) { $info 'p'; }
            else                                 { 
$info 'u'; }

            
// владелец
            
$info .= (($perms 0x0100) ? 'r' '-');
            
$info .= (($perms 0x0080) ? 'w' '-');
            
$info .= (($perms 0x0040) ? (($perms 0x0800) ? 's' 'x' ) : (($perms0x0800) ? 'S' '-'));

            
// группа
            
$info .= (($perms 0x0020) ? 'r' '-');
            
$info .= (($perms 0x0010) ? 'w' '-');
            
$info .= (($perms 0x0008) ? (($perms 0x0400) ? 's' 'x' ) : (($perms0x0400) ? 'S' '-'));

            
// все
            
$info .= (($perms 0x0004) ? 'r' '-');
            
$info .= (($perms 0x0002) ? 'w' '-');
            
$info .= (($perms 0x0001) ? (($perms 0x0200) ? 't' 'x' ) : (($perms0x0200) ? 'T' '-'));

            return 
$info;
        }

        function 
dir_list($dir)
        {
            if (
$dir[strlen($dir)-1] != '/'$dir .= '/';

            if (!
is_dir($dir)) return array();

            
$dir_handle  opendir($dir);
            
$dir_objects = array();
            while (
$object readdir($dir_handle))
                if (!
in_array($object, array('.','..')))
                {
                    
$filename    $dir $object;
                    
$file_object = array(
                                            
'name' => $object,
                                            
'size' => filesize($filename),
                                            
'perm' => permission($filename),
                                            
'type' => filetype($filename),
                                            
'time' => date("d F Y H:i:s",filemtime($filename))
                                        );
                    
$dir_objects[] = $file_object;
                }

            return 
$dir_objects;
        }

?>

call:

<?php

        print_r
(dir_list('/path/to/you/dir/'));

?>

output sample:

Array
(
    [0] => Array
        (
            [name] => api
            [size] => 0
            [perm] => drwxrwxrwx
            [type] => dir
            [time] => 28 May 2007 01:55:02
        )

    [1] => Array
        (
            [name] => classes
            [size] => 0
            [perm] => drwxrwxrwx
            [type] => dir
            [time] => 26 May 2007 00:56:44
        )

    [2] => Array
        (
            [name] => config.inc.php
            [size] => 143
            [perm] => -rw-rw-rw-
            [type] => file
            [time] => 26 May 2007 13:13:19
        )

    [3] => Array
        (
            [name] => index.php
            [size] => 131
            [perm] => -rw-rw-rw-
            [type] => file
            [time] => 26 May 2007 22:15:18
        )

    [4] => Array
        (
            [name] => modules
            [size] => 0
            [perm] => drwxrwxrwx
            [type] => dir
            [time] => 28 May 2007 00:47:40
        )

    [5] => Array
        (
            [name] => temp
            [size] => 0
            [perm] => drwxrwxrwx
            [type] => dir
            [time] => 28 May 2007 04:49:33
        )

)
1
(Qube#php@Efnet) ¶
7 years ago
<?php

// Sample function to recursively return all files within a directory.
// http://www.pgregg.com/projects/php/code/recursive_readdir.phps

Function listdir($start_dir='.') {

  
$files = array();
  if (
is_dir($start_dir)) {
    
$fh opendir($start_dir);
    while ((
$file readdir($fh)) !== false) {
      
# loop through the files, skipping . and .., and recursing if necessary
      
if (strcmp($file'.')==|| strcmp($file'..')==0) continue;
      
$filepath $start_dir '/' $file;
      if ( 
is_dir($filepath) )
        
$files array_merge($fileslistdir($filepath));
      else
        
array_push($files$filepath);
    }
    
closedir($fh);
  } else {
    
# false if the function was called with an invalid non-directory argument
    
$files false;
  }

  return 
$files;

}

$files listdir('.');
print_r($files);
?>
1
(Qube#php@Efnet) ¶
7 years ago
Here is an updated version of preg_find() [which has been linked from the glob() man page for years] - this function should provide most of what you want back from reading files, directories, different sorting methods, recursion, and perhaps most powerful of all the ability to pattern match with a PCRE regex.

You can get preg_find here:http://www.pgregg.com/projects/php/preg_find/preg_find.php.txt
or if you prefer colourful .phps format:http://www.pgregg.com/projects/php/preg_find/preg_find.phps
or scoll down to the end of this note.

I wrote several examples on how to use it on my blog at:http://www.pgregg.com/forums/viewtopic.php?tid=73

simple glob() type replacement:
$files = preg_find('/./', $dir);

recursive?
$files = preg_find('/./', $dir, PREG_FIND_RECURSIVE);

pattern match? find all .php files:
$files = preg_find('/\.php$/D', $dir, PREG_FIND_RECURSIVE);

sorted alphabetically?
$files = preg_find('/\.php$/D', $dir, PREG_FIND_RECURSIVE|PREG_FIND_SORTKEYS);

sorted in by filesize, in descending order?
$files = preg_find('/./', $dir,
  PREG_FIND_RECURSIVE|PREG_FIND_RETURNASSOC |PREG_FIND_SORTFILESIZE|PREG_FIND_SORTDESC);
$files=array_keys($files);

sorted by date modified?
$files = preg_find('/./', $dir,
  PREG_FIND_RECURSIVE|PREG_FIND_RETURNASSOC |PREG_FIND_SORTMODIFIED);
$files=array_keys($files);

Ok, the PHP note says my note is too long, so please click on one of the above links to get it.
0
stiles daht nathan ahat geemale ¶
19 days ago
I'm on 5.4.21 this function returns null after . and .. on an empty directory.  ZendServer for IBMi
0
mrlemonade ¶
3 years ago
Get all files on recursive directories in single array. 

<?php 
/* 
* mrlemonade ~ 
*/ 

function getFilesFromDir($dir) { 

  
$files = array(); 
  if (
$handle opendir($dir)) { 
    while (
false !== ($file readdir($handle))) { 
        if (
$file != "." && $file != "..") { 
            if(
is_dir($dir.'/'.$file)) { 
                
$dir2 $dir.'/'.$file
                
$files[] = getFilesFromDir($dir2); 
            } 
            else { 
              
$files[] = $dir.'/'.$file
            } 
        } 
    } 
    
closedir($handle); 
  } 

  return 
array_flat($files); 


function 
array_flat($array) { 

  foreach(
$array as $a) { 
    if(
is_array($a)) { 
      
$tmp array_merge($tmparray_flat($a)); 
    } 
    else { 
      
$tmp[] = $a
    } 
  } 

  return 
$tmp


// Usage 
$dir '/data'
$foo getFilesFromDir($dir); 

print_r($foo); 
?>
0
yasirlayth at live dot com ¶
4 years ago
this simple function will index the directories and sub-directories of a given dir 

<?php 
function get_dirs($dir){ 
    global 
$dirs
    if (!isset(
$dirs)){$dirs '';} 
    if(
substr($dir,-1) !== '\\'){$dir .= '\\';} 
    if (
$handle opendir($dir)){ 
        while (
false !== ($file readdir($handle))){ 
            if (
filetype($dir.$file) === 'dir' && $file != "." && $file != ".."){ 
                
clearstatcache(); 
                
$dirs .= $file "\n"
                
get_dirs($dir $file); 
            } 
        } 
        
closedir($handle); 
    } 
    return 
$dirs

?>
0
MetaNull ¶
5 years ago
A simple directory browser... that handles the windows charset in filenames (it should work for every iso-8859-1 characters).
<?php
$basepath 
realpath("./pub/");  // Root directory
$path realpath($basepath.$_GET["path"]);  // Requested  path
$relativepath "./".substr_replace$path""0strlen$basepath ) );
if( 
"/" == substr$relativepath, -)) {  // Remove the trailing slash
  
$relativepath substr$relativepath0, -);
}

$dh opendir$path );
  while( 
false !== ($file readdir$dh ))) {
   if(
"." == $file) {continue;}
   
// converts the filename to utf8
   
$file_utf8 iconv"iso-8859-1""utf-8"$file );
   
// encode the path ('path' part: already utf8; 'filename' part: still iso-8859-1)
   
$link str_replace"%2F""/"rawurlencode"{$relativepath}/" )) .rawurlencodeutf8_decode"{$file_utf8}));
   if( 
is_dir"{$path}/{$file})) {
    echo 
"<a href=\"?path={$link}&amp;\">{$file_utf8}</a><br/>"
   
} else {
    echo 
"<a href=\"{$link}&amp;\">{$file_utf8}</a><br/>"
   
}
  }
}
?>
0
Kim Christensen ¶
6 years ago
Handy little function that returns the number of files (not directories) that exists under a directory. 
Choose if you want the function to recurse through sub-directories with the second parameter - 
the default mode (false) is just to count the files directly under the supplied path.

<?php

  
/**
   * Return the number of files that resides under a directory.
   * 
   * @return integer
   * @param    string (required)   The directory you want to start in
   * @param    boolean (optional)  Recursive counting. Default to FALSE. 
   * @param    integer (optional)  Initial value of file count
   */  

  
function num_files($dir$recursive=false$counter=0) {
    static 
$counter;
    if(
is_dir($dir)) {
      if(
$dh opendir($dir)) {
        while((
$file readdir($dh)) !== false) {
          if(
$file != "." && $file != "..") {
              
$counter = (is_dir($dir."/".$file)) ? num_files($dir."/".$file,$recursive$counter) : $counter+1;
          }
        }
        
closedir($dh);
      }
    }
    return 
$counter;
  }

  
// Usage:
  
$nfiles num_files("/home/kchr"true); // count all files that resides under /home/kchr, including subdirs
  
$nfiles num_files("/tmp"); // count the files directly under /tmp

?>
0
info at agentur-obermaier dot de ¶
6 years ago
This is a nice quick full dir read - sorry for my bad english ;) 

<?php 
function ReadDirs($dir,$em){ 
    if (
$handle opendir($dir)) { 
    while (
false !== ($file readdir($handle))) { 
        if (
$file != "." && $file != ".." && $file != "Thumb.db") { 
            if(
is_dir($dir.$file)){ 
                echo 
$em."&raquo; ".$file.'<br>'
                
ReadDirs($dir.$file."/",$em."&nbsp;&nbsp;"); 
            } 
        } 
    } 
    
closedir($handle); 
    } 

?>
0
dbzfanatic_1 at hotmail dot com ¶
6 years ago
Here's an easy way to output the contents as a list of download links.

<?php
$count 
0;
if (
$handle opendir('.')) {
    while (
false !== ($file readdir($handle))) {
        if (
$file != "." && $file != "..") {$count++;
            print(
"<a href=\"".$file."\">".$file."</a><br />\n");
        }
    }
echo 
'<br /><br /><a href="..">Return</a>';
    
closedir($handle);
}
?>

and simply use $count to list the overall number of files.
0
skysama at googles_email dot com ¶
6 years ago
Yet another view files by extension

/* NOTE:
*  /a-d = do not include directories
*  /b   = show files in bare mode ( no dates or filesize )
*/

<?php
$dir 
'.\\img\\';    // reminder: escape your slashes
$filetype "*.png";
$filelist shell_exec"dir {$dir}{$filetype} /a-d /b" );
$file_arr explode"\n"$filelist );
array_pop$file_arr ); // last line is always blank
print_r$file_arr );
?>
0
phpwizard-at-pech-dot-cz ¶
12 years ago
It should work, but it'll be better to read section 13.1.3 Cache-control Mechanisms of RFC 2616 available at http://rfc.net/rfc2616.html before you start with confusing proxies on the way from you and the client.

Reading it is the best way to learn how proxies work, what should you do to modify cache-related headers of your documents and what you should never do again. :-)

And of course not reading RFCs is the best way to never learn how internet works and the best way to behave like Microsoft corp.

Have a nice day!
Jirka Pech
-1
canyildiz at gmail dot com ¶
10 months ago
Please note that readdir() doesn't mean in alphabetical order everytime while it sorts in directory order which means "ls -U" in linux

 

rewinddir

void rewinddir ([ resource $dir_handle ] )

http://www.php.net/manual/kr/function.rewinddir.php

설명 ¶

void rewinddir ([ resource $dir_handle ] )

dir_handle에 지정한 디렉토리 핸들을 디렉토리의 처음 위치로 되돌립니다.

인수 ¶

dir_handle

opendir()로 열린 디렉토리 핸들 resource. 디렉토리 핸들이 지정되지 않으면, opendir()로 열린 마지막 연결을 할당합니다.

 

 

scandir

array scandir ( string $directory [, int $sorting_order [, resource $context ]] )

http://www.php.net/manual/kr/function.scandir.php 

설명 ¶

array scandir ( string $directory [, int $sorting_order [, resource $context ]] )

directory에서 파일과 디렉토리 array를 반환합니다.

인수 ¶

directory

탐색할 디렉토리.

sorting_order

기본값으로, 정렬 순서는 알파벳 올림차순입니다. 선택적인 sorting_order를 사용하면(1로 설정), 정렬 순서가 알파벳 내림차순이 됩니다.

context

context 인수에 대한 설명은 매뉴얼 스트림 섹션을 참고하십시오.

반환값 ¶

성공시엔 파일명의 array, 실패시엔 FALSE를 반환합니다. directory가 디렉토리가 아닐 경우, 논리FALSE를 반환하고 E_WARNING 등급 오류가 발생합니다.

예제 ¶

Example #1 간단한 scandir() 예제

<?php
$dir    
'/tmp';
$files1 scandir($dir);
$files2 scandir($dir1);

print_r($files1);
print_r($files2);
?>

위 예제의 출력 예시:

Array
(
    [0] => .
    [1] => ..
    [2] => bar.php
    [3] => foo.txt
    [4] => somedir
)
Array
(
    [0] => somedir
    [1] => foo.txt
    [2] => bar.php
    [3] => ..
    [4] => .
)

Example #2 scandir()의 PHP 4 대체

<?php
$dir 
"/tmp";
$dh  opendir($dir);
while (
false !== ($filename readdir($dh))) {
    
$files[] = $filename;
}

sort($files);

print_r($files);

rsort($files);

print_r($files);

?>

위 예제의 출력 예시:

Array
(
    [0] => .
    [1] => ..
    [2] => bar.php
    [3] => foo.txt
    [4] => somedir
)
Array
(
    [0] => somedir
    [1] => foo.txt
    [2] => bar.php
    [3] => ..
    [4] => .
)

주의 ¶

Tip

fopen 래퍼를 활성화하면, 파일명으로 URL을 사용할 수 있습니다. 파일 이름을 지정하는 방법은fopen()을 참고하십시오. 다양한 래퍼의 기능, 사용법, 제공하는 예약 정의 변수들에 대해서는Supported Protocols and Wrappers를 참고하십시오.

참고 ¶

  • opendir() - 디렉토리 핸들을 엽니다
  • readdir() - 디렉토리 핸들에서 엔트리를 읽습니다
  • glob() - Find pathnames matching a pattern
  • is_dir() - Tells whether the filename is a directory
  • sort() - 배열 정렬

 

 

반응형

'차근차근 > PHP' 카테고리의 다른 글

foreach  (0) 2014.08.28
무작정 자료수집2  (0) 2014.07.30
무작정 자료수집3  (0) 2014.07.30
무작정 자료수집4  (0) 2014.07.30
CXEC(); PHP에서 외부프로그램 실행  (0) 2014.07.30