Policz ile plików w katalogu php

Pracuję nad nieco nowym projektem. Chciałem wiedzieć, jak to zrobić, aby liczyło się, ile plików znajduje się w danym katalogu.

<div id="header">
<?php 
    $dir = opendir('uploads/'); # This is the directory it will count from
    $i = 0; # Integer starts at 0 before counting

    # While false is not equal to the filedirectory
    while (false !== ($file = readdir($dir))) { 
        if (!in_array($file, array('.', '..') and !is_dir($file)) $i++;
    }

    echo "There were $i files"; # Prints out how many were in the directory
?>
</div>

To jest to, co mam do tej pory(od poszukiwania). Jednak nie pojawia się prawidłowo? Dodałem kilka uwag, więc nie krępuj się je usunąć, są po prostu po to, żebym mógł to zrozumieć najlepiej, jak Mogę.

Jeśli potrzebujesz więcej informacji lub czujesz się tak, jakbym nie opisał tego wystarczająco prosimy o stwierdzenie tak.

Author: Ruddy, 2012-10-09

11 answers

Możesz po prostu wykonać następujące czynności:

$fi = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS);
printf("There were %d Files", iterator_count($fi));
 219
Author: Baba,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-09 14:07:34

Możesz uzyskać liczbę plików w następujący sposób:

$directory = "/path/to/dir/";
$filecount = 0;
$files = glob($directory . "*");
if ($files){
 $filecount = count($files);
}
echo "There were $filecount files";

Gdzie "*" jest możesz zmienić to na określony typ pliku jeśli chcesz jak "*.jpg" lub możesz zrobić wiele typów plików jak ten:

glob($directory . "*.{jpg,png,gif}",GLOB_BRACE)

Flaga GLOB_BRACE rozszerza {a, b, c}, aby pasowała do "a", " b " lub " c "

 56
Author: JKirchartz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-09 14:05:36

Powinieneś mieć:

<div id="header">
<?php 
    // integer starts at 0 before counting
    $i = 0; 
    $dir = 'uploads/';
    if ($handle = opendir($dir)) {
        while (($file = readdir($handle)) !== false){
            if (!in_array($file, array('.', '..')) && !is_dir($dir.$file)) 
                $i++;
        }
    }
    // prints out how many were in the directory
    echo "There were $i files";
?>
</div>
 42
Author: Laurent Brieu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-09 13:49:30

Spróbuj tego.

// Directory
$directory = "/dir";

// Returns array of files
$files = scandir($directory);

// Count number of files and store them to variable..
$num_files = count($files)-2;

Nie licząc '."i"..'.

 35
Author: intelis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-23 22:32:49

Demo Robocze

<?php

$directory = "../images/team/harry/"; // dir location
if (glob($directory . "*.*") != false)
{
 $filecount = count(glob($directory . "*.*"));
 echo $filecount;
}
else
{
 echo 0;
}

?>
 12
Author: Nirav Ranpara,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-09 13:46:17

Ponieważ też tego potrzebowałem, byłem ciekaw, która alternatywa jest najszybsza.

Odkryłem, że -- jeśli chcesz tylko liczbę plików -- rozwiązanie Baby jest dużo szybsze niż inne. Byłem zaskoczony.

Wypróbuj to dla siebie:

<?php
define('MYDIR', '...');

foreach (array(1, 2, 3) as $i)
{
    $t = microtime(true);
    $count = run($i);
    echo "$i: $count (".(microtime(true) - $t)." s)\n";
}

function run ($n)
{
    $func = "countFiles$n";
    $x = 0;
    for ($f = 0; $f < 5000; $f++)
        $x = $func();
    return $x;
}

function countFiles1 ()
{
    $dir = opendir(MYDIR);
    $c = 0;
    while (($file = readdir($dir)) !== false)
        if (!in_array($file, array('.', '..')))
            $c++;
    closedir($dir);
    return $c;
}

function countFiles2 ()
{
    chdir(MYDIR);
    return count(glob("*"));
}

function countFiles3 () // Fastest method
{
    $f = new FilesystemIterator(MYDIR, FilesystemIterator::SKIP_DOTS);
    return iterator_count($f);
}
?>

Test run: (oczywiście, glob() nie liczy się dot-files)

1: 99 (0.4815571308136 s)
2: 98 (0.96104407310486 s)
3: 99 (0.26513481140137 s)
 12
Author: vbwx,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-12 21:02:13

Używam tego:

count(glob("yourdir/*",GLOB_BRACE))
 4
Author: Philipp Werminghausen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-07 05:45:12
<?php echo(count(array_slice(scandir($directory),2))); ?>

array_slice działa podobnie jak funkcja substr, tylko działa z tablicami.

Na przykład, to wyciąć dwa pierwsze klucze tablicy z tablicy:

$key_zero_one = array_slice($someArray, 0, 2);

I jeśli pominiesz pierwszy parametr, jak w pierwszym przykładzie, tablica nie będzie zawierać dwóch pierwszych par klucz / wartość *('."i"..').

 1
Author: Spooky,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-10 07:37:22

Może komuś się przyda. W systemie Windows możesz pozwolić Windows wykonać to zadanie, wywołując polecenie dir. Używam ścieżki absolutnej, jak E:/mydir/mysubdir.

<?php 
$mydir='E:/mydir/mysubdir';
$dir=str_replace('/','\\',$mydir);
$total = exec('dir '.$dir.' /b/a-d | find /v /c "::"');
 1
Author: Michel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-17 08:59:30
$it = new filesystemiterator(dirname("Enter directory here"));
printf("There were %d Files", iterator_count($it));
echo("<br/>");
    foreach ($it as $fileinfo) {
        echo $fileinfo->getFilename() . "<br/>\n";
    } 

To powinno zadziałać wprowadź katalog w dirname. i niech się stanie Magia.

 0
Author: oussama,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-16 10:32:15
  simple code add for file .php then your folder which number of file to count its      

    $directory = "images/icons";
    $files = scandir($directory);
    for($i = 0 ; $i < count($files) ; $i++){
        if($files[$i] !='.' && $files[$i] !='..')
        { echo $files[$i]; echo "<br>";
            $file_new[] = $files[$i];
        }
    }
    echo $num_files = count($file_new);

Proste dodaj swoje gotowe ....

 -2
Author: parajs dfsb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-02 13:37:53