Ver Mensaje Individual
  #2 (permalink)  
Antiguo 15/09/2007, 18:29
okram
Invitado
 
Mensajes: n/a
Puntos:
Re: Como hago para que un script lea todas las subcarpetas y archivos?

Hola, lo que debes hacer es crear una función recursiva, que calcule el tamaño total de los archivos en una carpeta, aplicando nuevamente la misma función a todas las supcarpetas que en ella se encuentren.

Estuve haciendo algunos arreglos a tu código, y me quedo esto:

Código PHP:
<?php 
$total
=26214400
if(
$total 1024 AND $total 1024*1024){ 
$totall="".(ceil($total/1024*100)/100) ." Kb"
}else 
if(
$total 1024*1024){ 
$totall="".(ceil($total/(1024*1024)*100)/100) ." Mb"
}else 
if(
$total 1024){ 
$totall="$totall Bytes"
}
$counters=0
$c=0
$num_files=0;  
$num_folders=0;
function 
size_readable ($size$retstring null) {
        
$sizes = array('bytes''kb''Mb''Gb''Tb''Pb''Eb''Zb''Yb');
        if (
$retstring === null) { $retstring '%01.2f %s'; }
        
$lastsizestring end($sizes);
        foreach (
$sizes as $sizestring) {
                if (
$size 1024) { break; }
                if (
$sizestring != $lastsizestring) { $size /= 1024; }
        }
        if (
$sizestring == $sizes[0]) { $retstring '%01d %s'; }
        return 
sprintf($retstring$size$sizestring);

function 
dir_tamano($path=".") { 
  global 
$counters$num_files$num_folders$c;
  
$directorio=dir($path);  
  while (
$archivo $directorio->read()) { 
    if(
$archivo !== "." and $archivo !== "..") { 
      if(!
is_dir($path."/".$archivo)) {
        
$num_files++; 
        
$filesize filesize($path."/".$archivo);
        
$counters=$counters+$filesize
        
$c=$c+$filesize
        echo 
$path."/".$archivo." - ".size_readable($filesize)."<br/>";  
      } else {
        
$num_folders++; 
        echo 
"<br /><b><span style=\"color: #f00;\">Directorio</span>: 
$path/$archivo<br /></b>"
;
        
dir_tamano($path."/".$archivo);
      }
    } 
  } 
  
$directorio->close(); 

dir_tamano();
if(
$counters 1024 AND $counters 1024*1024){ 
$counters="".(ceil($counters/1024*100)/100) ." Kb"
}else 
if(
$counters 1024*1024){ 
$counters="".(ceil($counters/(1024*1024)*100)/100) ." Mb"
}else 
if(
$counters 1024){ 
$counters="$counters Bytes"

echo
"El Tamaño Utilizado es de: $counters en $num_files Archivos ($num_folders 
Carpetas)"

$porcentaje=($c/$total)*100
$porcentaje=ceil($porcentaje); 
if(
$porcentaje 100){ 
$porcentaje=100

?>
<HTML>
<div align="center">Estas Ocupando el <b><?=$porcentaje?>%</b> de tu Disco 
  <table width="50%" border="3" cellpadding="0" cellspacing="0" bordercolor="#0000FF"> 
    <tr> 
      <td><img src="Imagenes/Web/giggle.jpg" width="<?=$porcentaje?>%" 
height="15"></td> 
    </tr> 
  </table><br> 
Ocupando: <b><?=$counters?></b> de <b><?=$totall?></b> 
</div>
Referencias:
http://www.php.net/filesize
http://www.php.net/isdir
http://www.sc.ehu.es/sbweb/fisica/cursoJava/numerico/recursivo/recursivo.htm
http://es.wikipedia.org/wiki/Funci%C3%B3n_recursiva

Un saludo,