2012-06-05 13 views
5

मुझे किसी निर्दिष्ट निर्देशिका में जेपीजी फ़ाइलों की कुल गणना प्राप्त करने की आवश्यकता है, जिसमें इसकी सभी उपनिर्देशिकाएं शामिल हैं। कोई उप-उप निर्देशिका नहीं।PHP निर्देशिका में कुल फ़ाइलों और उपनिर्देशिका फ़ंक्शन

संरचना इस तरह दिखता है:

 
dir1/ 
2 files 
    subdir 1/ 
     8 files 

कुल dir1 = 10 फ़ाइलें

 
dir2/ 
    5 files 
    subdir 1/ 
     2 files 
    subdir 2/ 
     8 files 

कुल dir2 = 15 फाइलें

मैं इस समारोह है, जो ऐसा नहीं करता है ठीक काम करें क्योंकि यह केवल अंतिम उपनिर्देशिका में फ़ाइलों की गणना करता है, और कुल वास्तविक से 2x अधिक है फाइलों का माउंट। (इच्छा उत्पादन 80 यदि मैं पिछले subdir में 40 फ़ाइलें) प्रत्येक छोरों के लिए

public function count_files($path) { 
global $file_count; 

$file_count = 0; 
$dir = opendir($path); 

if (!$dir) return -1; 
while ($file = readdir($dir)) : 
    if ($file == '.' || $file == '..') continue; 
    if (is_dir($path . $file)) : 
     $file_count += $this->count_files($path . "/" . $file); 
    else : 
     $file_count++; 
    endif; 
endwhile; 

closedir($dir); 
return $file_count; 
} 

उत्तर

6

इसके बारे में मस्ती के लिए मैं एक साथ इस मार पड़ी है:

class FileFinder 
{ 
    private $onFound; 

    private function __construct($path, $onFound, $maxDepth) 
    { 
     // onFound gets called at every file found 
     $this->onFound = $onFound; 
     // start iterating immediately 
     $this->iterate($path, $maxDepth); 
    } 

    private function iterate($path, $maxDepth) 
    { 
     $d = opendir($path); 
     while ($e = readdir($d)) { 
      // skip the special folders 
      if ($e == '.' || $e == '..') { continue; } 
      $absPath = "$path/$e"; 
      if (is_dir($absPath)) { 
       // check $maxDepth first before entering next recursion 
       if ($maxDepth != 0) { 
        // reduce maximum depth for next iteration 
        $this->iterate($absPath, $maxDepth - 1); 
       } 
      } else { 
       // regular file found, call the found handler 
       call_user_func_array($this->onFound, array($absPath)); 
      } 
     } 
     closedir($d); 
    } 

    // helper function to instantiate one finder object 
    // return value is not very important though, because all methods are private 
    public static function find($path, $onFound, $maxDepth = 0) 
    { 
     return new self($path, $onFound, $maxDepth); 
    } 
} 

// start finding files (maximum depth is one folder down) 
$count = $bytes = 0; 
FileFinder::find('.', function($file) use (&$count, &$bytes) { 
    // the closure updates count and bytes so far 
    ++$count; 
    $bytes += filesize($file); 
}, 1); 

echo "Nr files: $count; bytes used: $bytes\n"; 

आप आधार पथ गुजरती हैं, (-1 निष्क्रिय करने के लिए) हैंडलर और अधिकतम निर्देशिका गहराई पाया। पाया गया हैंडलर एक ऐसा फ़ंक्शन है जिसे आप बाहर परिभाषित करते हैं, यह find() फ़ंक्शन में दिए गए पथ से संबंधित पथ नाम पारित हो जाता है।

आशा है कि यह समझ में आता है और आपकी मदद करता है :)

-3

एक चाल और अधिक तेजी से ;-)

मुझे याद है के रूप में, opendir SplFileObject से derivated है कर सकता है कक्षा जो एक रिकर्सिवइटरेटर, ट्रैवर्सबल, इटरेटर, सेक्टेबल इटरेटर क्लास है, इसलिए, यदि आप एसपीएल मानक PHP लाइब्रेरी का उपयोग करते हैं तो पूरी छवियों को उपनिर्देशिका पर भी गिनने के लिए आपको थोड़ी देर की आवश्यकता नहीं है।

लेकिन, यह थोड़ी देर हो गया है कि मैंने PHP का उपयोग नहीं किया है, इसलिए मैंने गलती की हो सकती है।

6

आप का उपयोग RecursiveDirectoryIterator

<?php 
function scan_dir($path){ 
    $ite=new RecursiveDirectoryIterator($path); 

    $bytestotal=0; 
    $nbfiles=0; 
    foreach (new RecursiveIteratorIterator($ite) as $filename=>$cur) { 
     $filesize=$cur->getSize(); 
     $bytestotal+=$filesize; 
     $nbfiles++; 
     $files[] = $filename; 
    } 

    $bytestotal=number_format($bytestotal); 

    return array('total_files'=>$nbfiles,'total_size'=>$bytestotal,'files'=>$files); 
} 

$files = scan_dir('./'); 

echo "Total: {$files['total_files']} files, {$files['total_size']} bytes\n"; 
//Total: 1195 files, 357,374,878 bytes 
?> 
+0

धन्यवाद लॉरेंस!यह पूरी तरह से काम किया :) – Neoweiter

+0

कोई जांच नहीं। –

+0

@Neoweiter की सहायता करने में खुशी हुई है, यह उप-उप निर्देशिका भी स्कैन करता है, मैंने सोचा था कि आप विशेष रूप से केवल उप-डीआईआर स्तर तक चाहते थे? –

0
error_reporting(E_ALL); 

function printTabs($level) 
{ 
    echo "<br/><br/>"; 
    $l = 0; 
    for (; $l < $level; $l++) 
     echo "."; 
} 

function printFileCount($dirName, $init) 
{ 
    $fileCount = 0; 
    $st  = strrpos($dirName, "/"); 
    printTabs($init); 
    echo substr($dirName, $st); 

    $dHandle = opendir($dirName); 
    while (false !== ($subEntity = readdir($dHandle))) 
    { 
     if ($subEntity == "." || $subEntity == "..") 
      continue; 
     if (is_file($dirName . '/' . $subEntity)) 
     { 
      $fileCount++; 
     } 
     else //if(is_dir($dirName.'/'.$subEntity)) 
     { 
      printFileCount($dirName . '/' . $subEntity, $init + 1); 
     } 
    } 
    printTabs($init); 
    echo($fileCount . " files"); 

    return; 
} 

printFileCount("/var/www", 0); 

बस, जाँच की यह काम कर रहा है इस तरह यह कर सकता है। लेकिन परिणामों का संरेखण खराब है, तर्क

1

यदि कोई भी फाइलों और निर्देशिकाओं की कुल संख्या को गिनने की तलाश में है।

दिखाएँ/कुल निर्देशिका और उप निर्देशिका गिनती

find . -type d -print | wc -l 

दिखाएँ गिनती/गिनती मुख्य में फ़ाइलों की कुल संख्या और उप निर्देशिका

find . -type f -print | wc -l 

दिखाएँ/(कोई उप केवल वर्तमान निर्देशिका से फाइल गिनती dir) वर्तमान dir में कुल निर्देशिका और फ़ाइलें (कोई उप निर्देशिका

find . -maxdepth 1 -type f -print | wc -l 

दिखाएँ/गिनती)

ls -1 | wc -l 
संबंधित मुद्दे

 संबंधित मुद्दे