Header Ads

PHP 디렉토리 함수 정리


/*
opendir, closedir
resource = opendir(string_path)
void closedir(resource_dir_handle)
http://php.net/manual/en/function.opendir.php
http://php.net/manual/en/function.closedir.php
*/
$dir = "../public_html/";
// 알고 있는 디렉토리를 열어서, 내용을 읽어들이는 작업입니다.
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            echo "filename: $file : filetype: " . filetype($dir.$file);
        }
        closedir($dh);
    }
}

/*
readdir
string = readdir(resource_dir_hendle)
http://php.net/manual/en/function.readdir.php
*/
if ($handle = opendir('test/')) {
    echo "Directory handle : " . $handle;
    echo "Files : ";

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

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

    closedir($handle); 
}

/*
rewinddir
void = rewinddir(resource_dir_handle)
http://php.net/manual/en/function.rewinddir.php
*/
if ($handle = opendir("test")) {
 // 1st
 $filename = readdir($handle);
 echo " file name : " . $filename . "
"; // 2nd $filename = readdir($handle); echo " file name : " . $filename . "
"; // 3rd $filename = readdir($handle); echo " file name : " . $filename . "
"; // 4th $filename = readdir($handle); echo " file name : " . $filename . "
"; // 5th $filename = readdir($handle); echo " file name : " . $filename . "
"; // rewind dir rewinddir($handle); // go to 1st $filename = readdir($handle); echo " file name : " . $filename . "
"; // close dir closedir($handle); } /* chdir bool = chdir(string_directory) http://php.net/manual/en/function.chdir.php */ // 현재 디렉토리 echo getcwd(); // 바뀐 디렉토리 chdir('/public_html/test/'); echo getcwd(); /* dir Class dir(string directory) http://php.net/manual/en/function.dir.php */ $d = dir("css"); echo "Handle: " . $d->handle . "
"; echo "Path: " . $d->path . "
"; while (false !== ($entry = $d->read())) { echo $entry."
"; } $d->close(); /* scandir array = scandir(string_directory, int_sorting_order) http://php.net/manual/en/function.scandir.php */ $dir = 'css'; $files1 = scandir($dir); //asc $files2 = scandir($dir, 1); // desc print_r($files1); print_r($files2);

No comments

Copyright 2016, minki. All rights reserved. Powered by Blogger.