【问题标题】:Listing all directories (on hard-disk) and its folders and files列出所有目录(在硬盘上)及其文件夹和文件
【发布时间】:2016-04-13 11:41:17
【问题描述】:

有没有一种方法可以读取我 PC 上的整个文件系统?我试图列出W: 目录中的所有文件,但代码不起作用。

<?php
 $fso = new COM('Scripting.FileSystemObject'); 
    $D = $fso->Drives; 
    $type = array("Unknown","Removable","Fixed","Network","CD-ROM","RAM Disk"); 
    foreach($D as $d ){ 
       $dO = $fso->GetDrive($d); 
       $s = ""; 
       if($dO->DriveType == 3){ 
           $n = $dO->Sharename; 
       }else if($dO->IsReady){ 
           $n = $dO->VolumeName; 
           $s = file_size($dO->FreeSpace) . " free of: " . file_size($dO->TotalSize); 
       }else{ 
           $n = "[Drive not ready]"; 
       } 
   echo "Drive " . $dO->DriveLetter . ": - " . $type[$dO->DriveType] . " - " . $n . " - " . $s . "<br>"; 
   $dir = $dO->DriveLetter;

   if (is_dir($dir.':\\')){ // NEVER ENTERS THE IF BLOCK
    if ($dh = opendir($dir)){
      while (($file = readdir($dh)) !== false){
        echo "filename:" . $file . "<br>";
      }
      closedir($dh);
    }
  }

    } 

      function file_size($size) 
      { 
      $filesizename = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"); 
      return $size ? round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $filesizename[$i] : '0 Bytes'; 
      } 


?> 

在上面的代码中is_dir 调用总是失败。这可能是什么原因? windows文件系统的正确读取方式是什么?

【问题讨论】:

  • 我认为这个论坛上已经没有一个人使用 Windows 开发 PHP 应用程序了。运气不好,你可能应该切换到linux....还有为什么???? Python 的操作系统 API 比 PHP 好得多……不管你在做什么……Python 可能更合适。

标签: php windows directory


【解决方案1】:

试试这个http://php.net/manual/en/class.recursivedirectoryiterator.php

<?php

$path = realpath('/etc');

$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $name => $object){
    echo "$name\n";
}

【讨论】:

    猜你喜欢
    • 2012-09-02
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    • 2011-04-20
    相关资源
    最近更新 更多