2

次のコードは、フォルダー内のファイルの数をカウントします。

<?php
function folderlist(){
  $directoryist = array();
$startdir = './';
//get all image files with a .jpg extension.
  $ignoredDirectory[] = '.';
  $ignoredDirectory[] = '..';
   if (is_dir($startdir)){
       if ($dh = opendir($startdir)){
           while (($folder = readdir($dh)) !== false){
               if (!(array_search($folder,$ignoredDirectory) > -1)){
                 if (filetype($startdir . $folder) == "dir"){
                       $directorylist[$startdir . $folder]['name'] = $folder;
                       $directorylist[$startdir . $folder]['path'] = $startdir;
                   }
               }
           }
           closedir($dh);
       }
   }
return($directorylist);
}


$folders = folderlist();

$total_files = 0;
foreach ($folders as $folder){
    $path = $folder['path'];
    $name = $folder['name'];
    $count = iterator_count(new DirectoryIterator($path . $name));
    $total_files += $count;

    echo '<li>';
    echo '<a href="' .$path .'index.php?album=' .$name . '" class="style1">';
    echo '<strong>' . $name . '</strong>';
    echo ' (' . $count . ' files found)';
    echo '</a>';
    echo '</li>';
}
  echo "Total Files:". $total_files;
?>

ただし、何らかの理由でカウントが2ずれています。13個のファイルがあるフォルダーがありますが、このコードはカウントを15として返します。空のフォルダーの場合、これは2個のカウントを返します。

上記のスニペットの問題を誰かが私に指摘できますか?

4

2 に答える 2

4

DirectoryIteratorでやっています

$files_in_directory = new DirectoryIterator($path_to_folder);
$c = 0;

foreach($files_in_directory as $file)
{
    // We want only files
    if($file->isDot()) continue;
    if($file->isDir()) continue;
    $c++;
}

var_dump($c);

関数として使用する場合:

function folderlist($directories = array(), $extensions = array())
{
    if(empty($directories))
        return false;

    $result      = array();
    $total_count = 0;
    foreach($directories as $directory)
    {
        $files_in_directory = new DirectoryIterator($directory);
        $c                  = 0;

        foreach($files_in_directory as $file)
        {
            // We want only files
            if($file->isDot()) continue;
            if($file->isDir()) continue;

            // This is for php < 5.3.6
            $file_extension = pathinfo($file->getFilename(), PATHINFO_EXTENSION);

            // If you have php >= 5.3.6 you can use following instead
            // $file_extension = $fileinfo->getExtension()

            if(in_array($file_extension, $extensions)){
                $c++;
                $result['directories'][$directory]['files'][$c]['name'] = $file->getFilename();
                $result['directories'][$directory]['files'][$c]['path'] = $file->getPath();
                $result['directories'][$directory]['count']             = $c;
            }     
        }
        $total_count += $c;    
    }

    $result['total_count'] = $total_count;

    return $result;    

}

GETスーパーグローバルに基づく結果の表示:

if(isset($_GET['album']) && !empty($_GET['album']) && !isset($_GET['listfiles']))
    {
        // We are in directory view mode
        $album = $_GET['album'];

        // View all directories and their file count for specified album
        $view_directory = folderlist(array($album), array('jpeg', 'jpg', 'log'));

        // Loop the folders and display them with file count
        foreach ($view_directory['directories'] as $folder_name => $folder_files){

            $count = $folder_files['count'];
            echo '<li>';
            echo '<a href="files.php?album=' . $folder_name . '&listfiles=1" class="style1">';
            echo '<strong>' . basename($folder_name) . '</strong>';
            echo ' (' . $count . ' files found)';
            echo '</a>';
            echo '</li>';  

        }

        echo "Total Files:". $get_dir_info['total_count'];   

    }
    elseif(isset($_GET['album'], $_GET['listfiles']) && !empty($_GET['album']))
    {
        // We are in file view mode for folder
        $album = $_GET['album'];

        // View all files in directory
        $view_files = folderlist(array($album), array('jpeg', 'jpg', 'log'));   

        echo 'Showing folder content of: <b>'.basename($album).'</b>';
        foreach($view_files['directories'][$album]['files'] as $file)
        {     

            $path = $file['path'];
            $name = $file['name'];

            echo '<li>';
            echo '<a href="files.php?file=' . $name . '&path=' . $path . '" class="style1">';
            echo '<strong>' . $name . '</strong>';
            echo '</a>';
            echo '</li>';   
        } 
    }
于 2012-11-18T13:21:09.197 に答える
0

この行はまだ「。」を返しています。および'..'変数。

if (!(array_search($folder,$ignoredDirectory) > -1))

参照: http: //php.net/manual/en/language.types.boolean.php

試す

if (!array_search($folder,$ignoredDirectory))

編集:

また、これを変更します。

$ignoredDirectory[] = '.';
$ignoredDirectory[] = '..';

$ ignoredDirectory = array('。'、'..');

于 2012-11-18T12:56:49.843 に答える