dir

(PHP 4, PHP 5, PHP 7, PHP 8)

dir β€” Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ экзСмпляр класса Directory

ОписаниС

function dir(string $directory, ?resource $context = null): Directory|false

ПсСвдо-ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π½ΠΎ-ΠΎΡ€ΠΈΠ΅Π½Ρ‚ΠΈΡ€ΠΎΠ²Π°Π½Π½Ρ‹ΠΉ ΠΌΠ΅Ρ…Π°Π½ΠΈΠ·ΠΌ для чтСния ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³Π°. ΠžΡ‚ΠΊΡ€Ρ‹Π²Π°Π΅Ρ‚ΡΡ ΠΏΠ΅Ρ€Π΅Π΄Π°Π½Π½Ρ‹ΠΉ Π² ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π΅ directory ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

directory

ΠšΠ°Ρ‚Π°Π»ΠΎΠ³ для открытия

context
РСсурс (resource) контСкста ΠΏΠΎΡ‚ΠΎΠΊΠ°.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ экзСмпляр класса Directory ΠΈΠ»ΠΈ false Π² случаС возникновСния ошибки.

Бписок измСнСний

ВСрсия ОписаниС
8.0.0 context Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ допускаСт Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ null.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ использования dir()

ΠžΠ±Ρ€Π°Ρ‚ΠΈΡ‚Π΅ Π²Π½ΠΈΠΌΠ°Π½ΠΈΠ΅ Π½Π° способ, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΌ осущСствляСтся ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° значСния, Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΠΎΠ³ΠΎ Directory::read() Π² ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π΅, ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠΌ Π½ΠΈΠΆΠ΅. Π’ этом ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π΅ явно проводится ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° значСния Π½Π° ΠΈΠ΄Π΅Π½Ρ‚ΠΈΡ‡Π½ΠΎΡΡ‚ΡŒ (выраТСния ΠΈΠ΄Π΅Π½Ρ‚ΠΈΡ‡Π½Ρ‹, ΠΊΠΎΠ³Π΄Π° ΠΎΠ½ΠΈ Ρ€Π°Π²Π½Ρ‹ ΠΈ ΡΠ²Π»ΡΡŽΡ‚ΡΡ ΠΎΠ΄Π½ΠΎΠ³ΠΎ Ρ‚ΠΈΠΏΠ° - Π·Π° Π±ΠΎΠ»Π΅Π΅ ΠΏΠΎΠ΄Ρ€ΠΎΠ±Π½ΠΎΠΉ ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΠ΅ΠΉ ΠΎΠ±Ρ€Π°Ρ‚ΠΈΡ‚Π΅ΡΡŒ ΠΊ Π³Π»Π°Π²Π΅ ΠžΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€Ρ‹ сравнСния) Π·Π½Π°Ρ‡Π΅Π½ΠΈΡŽ false, ΠΏΠΎΡΠΊΠΎΠ»ΡŒΠΊΡƒ Π² ΠΈΠ½ΠΎΠΌ случаС, любой элСмСнт ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³Π°, Ρ‡ΡŒΡ‘ имя ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ Π²Ρ‹Ρ€Π°ΠΆΠ΅Π½ΠΎ ΠΊΠ°ΠΊ false, остановит Ρ†ΠΈΠΊΠ».

<?php
$d
= dir("/etc/php5");
echo
"ДСскриптор: " . $d->handle . "\n";
echo
"ΠŸΡƒΡ‚ΡŒ: " . $d->path . "\n";
while (
false !== ($entry = $d->read())) {
echo
$entry."\n";
}
$d->close();
?>

Π’Ρ‹Π²ΠΎΠ΄ ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π° Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΠΎΡ…ΠΎΠΆ Π½Π°:

ДСскриптор: Resource id #2
ΠŸΡƒΡ‚ΡŒ: /etc/php5
.
..
apache
cgi
cli

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅:

ΠŸΠΎΡ€ΡΠ΄ΠΎΠΊ, Π² ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌ ΠΌΠ΅Ρ‚ΠΎΠ΄ "read" Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ элСмСнты ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³Π°, зависит ΠΎΡ‚ ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΎΠ½Π½ΠΎΠΉ систСмы.

οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 6 notes

up
7
synnus at gmail dot com ΒΆ
5 years ago
<?php

   // simple juste use FilesystemIterator 
   // and you can skip dot and duble dot
   // and use it in array
   // new FilesystemIterator( PATH , OPTIONS ) : array 

$array_file_list = new FilesystemIterator( PATH_ROOT . 'folder/', FilesystemIterator::SKIP_DOTS );

?>
up
7
fordiman at gmail dot com ΒΆ
20 years ago
This one's pretty nice.  After getting frustrated for hunting down .jpg files in my massive music collection (PHP would run out of memory), I thought there should be a preg_ls function.

function preg_ls ($path=".", $rec=false, $pat="/.*/") {
    // it's going to be used repeatedly, ensure we compile it for speed.
    $pat=preg_replace("|(/.*/[^S]*)|s", "\\1S", $pat);
    //Remove trailing slashes from path
    while (substr($path,-1,1)=="/") $path=substr($path,0,-1);
    //also, make sure that $path is a directory and repair any screwups
    if (!is_dir($path)) $path=dirname($path);
    //assert either truth or falsehoold of $rec, allow no scalars to mean truth
    if ($rec!==true) $rec=false;
    //get a directory handle
    $d=dir($path);
    //initialise the output array
    $ret=Array();
    //loop, reading until there's no more to read
    while (false!==($e=$d->read())) {
        //Ignore parent- and self-links
        if (($e==".")||($e=="..")) continue;
        //If we're working recursively and it's a directory, grab and merge
        if ($rec && is_dir($path."/".$e)) {
            $ret=array_merge($ret,preg_ls($path."/".$e,$rec,$pat));
            continue;
        }
        //If it don't match, exclude it
        if (!preg_match($pat,$e)) continue;
        //In all other cases, add it to the output array
        $ret[]=$path."/".$e;
    }
    //finally, return the array
    return $ret;
}

Not bad for a mere 18 lines, don't you think?

Example use:

foreach (preg_ls("/etc/X11", true, "/.*\.conf/i") as $file) echo $file."\n";

Output: 

/etc/X11/xkb/README.config
/etc/X11/xorg.conf-vesa
/etc/X11/xorg.conf~
/etc/X11/gui.conf
/etc/X11/xorg.conf
/etc/X11/xorg.conf-fbdev
up
3
samuel dot l at mushicrew dot com ΒΆ
20 years ago
Note that the dir object will use the default encoding for non-unicode programs on Windows with PHP 5.x.

So, if you have a file named with characters unsupported by the current default encoding, the dir->read() method will return a wrong entry.

<?php
/*
** This script is on the same directory than a file named with
** unsupported characters for the current default encoding.
*/
$d = dir("./");
while(false !== ($e = $d->read()))
    echo $e . '<br/>';
?>

This will print a "?" for every unsupported characters, and not the right file name. So take care if you check with is_file/is_dir right after enumerating.
up
2
Anonymous ΒΆ
20 years ago
Regarding samuel's comment about the dir() function not supporting Unicode properly, it's all in the encoding. The function does NOT internally change Unicode characters into question marks (?), as I was first led to believe. If you simply try to output them in UTF-8, they'll show up just right.
up
1
synnus at gmail dot com ΒΆ
4 years ago
<?php

/*
  New recursive PHP8 
  gen array path with  FilesystemIterator
*/

$recurcive_path = [];
rdir(path, $recurcive_path);
var_dump($recurcive_path);

function rdir(string $path, array &$recurcive_path): string
{
    if ($path != '') {
        $recurcive_path[] = $path;
        $array_list = iterator_to_array(new FilesystemIterator($path, FilesystemIterator::SKIP_DOTS));
        foreach ($array_list as $name) {
            $pathname = $name->getpathname();
            if(is_dir($pathname) && $name->getfilename()[0] != '.'){
                $path = rdir($pathname,$recurcive_path);
            }
        }
        return $path;
    }
    return '';
}

?>
up
-1
GUILLE@GARGANO ΒΆ
15 years ago
to get a dir of http://www.example.com/directory

<?php
function remotedir($dir)
{
  $dir = str_replace(" ", "%20", html_entity_decode($dir));
  if (($rh = fopen($dir, 'rb')) === FALSE) { return false; }
  $i = 0;
  while (!feof($rh)) {
     $archivos = fgetss($rh);
     $directorio[$i++] = trim( substr($archivos,1,strpos($archivos," ",1)) );
  }
  fclose($rh);
  return $directorio;
}
?>