Bitacoreando

13/8/2005

Listados de archivos con PHP

 

<?PHP
/****************************************************************************

files_in_dir.php Listados de archivos

Copyright (c) 2005, Rubén Marrero <ruben@lingo.com.mx> All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
    * this list of conditions and the following disclaimer.  Redistributions in
    * binary form must reproduce the above copyright notice, this list of
    * conditions and the following disclaimer in the documentation and/or other
    * materials provided with the distribution.  Neither the name of the
    * LINGO SYSTEMS nor the names of its contributors may be used to endorse
    * or promote products derived from this software without specific prior
    * written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/

$obj_busca = new files("My_files");
$obj_busca->buscaArchivos();
$obj_busca->armaHtml();
echo
$obj_busca->html;

/***********************************************************************/

class files{
    
// Etiquetas de los tipos de archivos a buscar, junto con
    // alguna parte reconocible del nombre de archivo.
    
var $search_for = Array(
            
‘Documentos OOo’=>Array(
                
’sxw’,
                
’sxc’
                
),
            
‘Videos’=>Array(
                
‘mpg’,
                
‘wmv’,
                
‘mov’,
                
‘asf’,
                
‘mpeg’
                
),
            
‘Documentos HTML’=>Array(
                
‘htm’
                
),
            
‘Archivos MP3′=>Array(
                
‘mp3′
                
)
            );
    var
$label_otros = "ZZ_Otros Archivos";

function files($ruta){
// Donde se realiza la búsqueda?
define (‘FILE_PATH’,$ruta);

// del constructor

}

function buscaArchivos(){
if (
is_dir(FILE_PATH)){
  
$the_dir = opendir(FILE_PATH);
  while (
false != ($file = readdir($the_dir))){
      
// $file contiene cada archivo y directorio que está en el directorio FILE_PATH
      // Buscamos en el arreglo $search_for
    
foreach($this->search_for as $label =>$arr_exts){
    
$disk_file =‘./’.FILE_PATH."/$file";
    
// todas las extensiones, con modificador de
    // case-insensitive
    
$regexp = "/".join(‘|’,$arr_exts)."/i";
     if (
preg_match_all($regexp,$file,$arr_tirame) ){
      
$this->arr_found[$label][$file]=filectime($disk_file);
      continue(
2); // si lo encontramos, dejamos de buscar.. no funciona muy bien, creo
    
}//del if preg_match
    
    
}//del foreach search for

    // si estamos aqui, es que no encontramos nada
    // en ninguno de los arreglos de extensiones
    
if (isset($disk_file) && is_file($disk_file)){
     
$this->arr_found[$this->label_otros][$file]=filectime($disk_file);
    }
    
  }
// del while    
closedir($the_dir);
}else{
    echo
"la ruta ".FILE_PATH." no es un directorio";
}
// del if is_dir
} // de la función muestra_archivos
// En este punto, $arr_found tiene todo.

function armaHtml(){
$this->html = "<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
<title>Archivos en el directorio "
.FILE_PATH." el ".date(‘d/F/Y H:i:s’,time())."</title>
<style>
*{
  font-family: Bitstream Vera Sans, Verdana, Helvetica, Arial;
}
.date,a{
  font-size: x-small;
}
h2{
  color: White;
  background-color: blue;
}
</style>
</head>
<body>
"
;

//existen el arreglo sobre el que
//vamos a trabajar?
if (!isset($this->arr_found)){
$this->html .= "<h2> No se ha realizado la búsqueda en ".FILE_PATH."</h2>\n";
}

// Ahora ordenamos las etiquetas.
asort($this->arr_found,SORT_STRING);

$cnt_new = 0;

// Iteramos para cada etiqueta
foreach($this->arr_found as $label=>$arr_files){
$this->html .= "<h2>$label</h2>
<blockquote>"
;
// ordenamos de forma descendente los
// nombres de archivo baje esta eqtiqueta
arsort($arr_files);
  
// y para cada uno, armamos el HTML.
  
foreach($arr_files as $file=>$time){
    
$date =date(‘d/m/Y H:i’,$time);
    
// Si la fecha del archivo es igual a la de hoy, entonces lo escribimos
    
$date = (date(‘d/m/Y’,$time) == date(‘d/m/Y’,time())) ?‘HOY’: $date;
    
// marcamos los primeros cuatro archivos como nuevos
    
$new = ($cnt_new <= 4)? ‘<img src="icon_new.gif">’:;
    
$this->html .= "$new <a href=’".FILE_PATH."/$file’>$file</a> <span class=’date’>$date</span><br/>\n";
    
$cnt_new++;
  }
$cnt_new =0;
$this->html .= "</blockquote>";
}
// del foreach
$this->html .= "</body></html>";
}
// de arma_html;
} // de la clase files

?>

Filed under: Bitacoreando — ruben @ 4:58 pm

1 Comment »

  1. no sirve ese pinche file, ya lo probe, checalo.

    Comment by anonimoNo Gravatar — 14/1/2008 @ 10:52 am


RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress