Visualizzazione dei risultati da 1 a 8 su 8

Discussione: sottocartelle

  1. #1
    Utente bannato
    Registrato dal
    Oct 2008
    Messaggi
    88

    sottocartelle

    io ho questo codice

    <?php
    foreach(glob("dati/*.html") as $file) {

    $code = file_get_contents($file);

    cosi estraggo tutte le pagine html contenute in dati

    tuttavia ci sono delle sottocartelle in dati come sarebbe possibile che la funzioni cerchi anche nelle sottocartelle?

  2. #2
    Devi fare una funzione ricorsiva, attento però a non andare in timeout.

  3. #3
    Utente di HTML.it L'avatar di dottwatson
    Registrato dal
    Feb 2007
    Messaggi
    3,012
    prova con questa funzione

    Codice PHP:
    $files=list_elements('dati','files');
    foreach(
    $files as $file
    {
    $blocks=explode('.',$file)
    $ext array_pop($blocks);
    if (
    $ext =='html')  $code file_get_contents($file);
    }




    function 
    list_elements($dir,$out
      {
      
    $file_list '';
      
    $stack[] = $dir;
      while (
    $stack
        {
        
    $current_dir array_pop($stack);
        if (
    $dh opendir($current_dir)) 
          {
          while ((
    $file readdir($dh)) !== false
            {
            if (
    $file !== '.' AND $file !== '..'
              {
              
    $current_file "{$current_dir}/{$file}";
              if (
    is_file($current_file)) 
                {
                
    $file_list[] = "{$current_dir}/{$file}";
                } 
              elseif (
    is_dir($current_file)) 
                {
                
    $stack[] = $current_file;
                
    $dirs[]=$current_file;
                }
              }
            }
          }
        }
      if      (
    $out=='file') return $file_list;
      elseif  (
    $out=='dir' ) return $dirs;
      } 
    Non sempre essere l'ultimo è un male... almeno non devi guardarti le spalle

    il mio profilo su PHPClasses e il mio blog laboweb

  4. #4
    Utente bannato
    Registrato dal
    Oct 2008
    Messaggi
    88

    hum

    hum ok grazie come posso aggiungerla correttamente a questo codice???

    Me lo potresti postare??

    Codice PHP:
    <?php
    foreach(glob("*.html") as $file) {

    $code file_get_contents($file);

    $titolo '';
    $descrizione '';

    if(
    preg_match('@<title>([^<]*?)</title>@'$code$match)){
    $titolo $match[1];
    }

    if(
    preg_match('@<meta name="description" content="([^"]*?)">@'$code$match)){
    $descrizione $match[1];
    }

    echo 
    "$file : $titolo , $descrizione
    "
    ;
    }
    ?>

  5. #5
    Utente di HTML.it L'avatar di dottwatson
    Registrato dal
    Feb 2007
    Messaggi
    3,012
    forse così?
    Codice PHP:
    $files=list_elements('dati','files');
    foreach(
    $files as $file)
      {
      
    $blocks=explode('.',$file)
      
    $ext array_pop($blocks);
      if (
    $ext =='html')  
        {
        
    $code file_get_contents($file);
        
    $titolo '';
        
    $descrizione '';
        
        if(
    preg_match('@<title>([^<]*?)</title>@'$code$match)){
        
    $titolo $match[1];
        }
        
        if(
    preg_match('@<meta name="description" content="([^"]*?)">@'$code$match)){
        
    $descrizione $match[1];
        }
        
        echo 
    "$file : $titolo , $descrizione
    "
    ;
        }
      }




    function 
    list_elements($dir,$out)
      {
      
    $file_list '';
      
    $stack[] = $dir;
      while (
    $stack)
        {
        
    $current_dir array_pop($stack);
        if (
    $dh opendir($current_dir))
          {
          while ((
    $file readdir($dh)) !== false)
            {
            if (
    $file !== '.' AND $file !== '..')
              {
              
    $current_file "{$current_dir}/{$file}";
              if (
    is_file($current_file))
                {
                
    $file_list[] = "{$current_dir}/{$file}";
                }
              elseif (
    is_dir($current_file))
                {
                
    $stack[] = $current_file;
                
    $dirs[]=$current_file;
                }
              }
            }
          }
        }
      if      (
    $out=='file') return $file_list;
      elseif  (
    $out=='dir' ) return $dirs;
      } 
    Non sempre essere l'ultimo è un male... almeno non devi guardarti le spalle

    il mio profilo su PHPClasses e il mio blog laboweb

  6. #6
    Utente bannato
    Registrato dal
    Oct 2008
    Messaggi
    88

    ma cmq

    mi da questo errore
    Parse error: syntax error, unexpected T_VARIABLE in C:\wamp\www\1.php on line 6

    il codice e` questo ovviamente

    Codice PHP:
    <?php
    $files
    =list_elements('dati','files');
    foreach(
    $files as $file)
      {
     
    $blocks=explode('.',$file)
     
    $ext array_pop($blocks);
      if (
    $ext =='html')  
        {
        
    $code file_get_contents($file);
        
    $titolo '';
        
    $descrizione '';
        
        if(
    preg_match('@<title>([^<]*?)</title>@'$code$match)){
        
    $titolo $match[1];
        }
        
        if(
    preg_match('@<meta name="description" content="([^"]*?)">@'$code$match)){
        
    $descrizione $match[1];
        }
        
        echo 
    "$file : $titolo , $descrizione
    "
    ;
        }
      }




    function 
    list_elements($dir,$out)
      {
      
    $file_list '';
      
    $stack[] = $dir;
      while (
    $stack)
        {
        
    $current_dir array_pop($stack);
        if (
    $dh opendir($current_dir))
          {
          while ((
    $file readdir($dh)) !== false)
            {
            if (
    $file !== '.' AND $file !== '..')
              {
              
    $current_file "{$current_dir}/{$file}";
              if (
    is_file($current_file))
                {
                
    $file_list[] = "{$current_dir}/{$file}";
                }
              elseif (
    is_dir($current_file))
                {
                
    $stack[] = $current_file;
                
    $dirs[]=$current_file;
                }
              }
            }
          }
        }
      if      (
    $out=='file') return $file_list;
      elseif  (
    $out=='dir' ) return $dirs;
      }
      
    ?>

  7. #7
    Utente di HTML.it L'avatar di dottwatson
    Registrato dal
    Feb 2007
    Messaggi
    3,012
    Codice PHP:
    $blocks=explode('.',$file); 
    mancava il punto e virgola...

    mi chiedo se sto parlando con qualcuno che sappia cosa sia il php e come si scrive, oppure che viva di copia incolla... :master:


    ma a giudicare dall' errore segnalato sembra che viva di copia incolla
    Non sempre essere l'ultimo è un male... almeno non devi guardarti le spalle

    il mio profilo su PHPClasses e il mio blog laboweb

  8. #8
    Utente bannato
    Registrato dal
    Oct 2008
    Messaggi
    88
    errore:

    Warning: opendir(dati) [function.opendir]: failed to open dir: No error in C:\wamp\www\1.php on line 35

    Warning: Invalid argument supplied for foreach() in C:\wamp\www\1.php on line 3


    Codice PHP:
    <?php
    $files
    =list_elements('dati','files');
    foreach(
    $files as $file)
      {
      
    $blocks=explode('.',$file);
      
    $ext array_pop($blocks);
      if (
    $ext =='html')  
        {
        
    $code file_get_contents($file);
        
    $titolo '';
        
    $descrizione '';
        
        if(
    preg_match('@<title>([^<]*?)</title>@'$code$match)){
        
    $titolo $match[1];
        }
        
        if(
    preg_match('@<meta name="description" content="([^"]*?)">@'$code$match)){
        
    $descrizione $match[1];
        }
        
        echo 
    "$file : $titolo , $descrizione
    "
    ;
        }
      }




    function 
    list_elements($dir,$out)
      {
      
    $file_list '';
      
    $stack[] = $dir;
      while (
    $stack)
        {
        
    $current_dir array_pop($stack);
        if (
    $dh opendir($current_dir))
          {
          while ((
    $file readdir($dh)) !== false)
            {
            if (
    $file !== '.' AND $file !== '..')
              {
              
    $current_file "{$current_dir}/{$file}";
              if (
    is_file($current_file))
                {
                
    $file_list[] = "{$current_dir}/{$file}";
                }
              elseif (
    is_dir($current_file))
                {
                
    $stack[] = $current_file;
                
    $dirs[]=$current_file;
                }
              }
            }
          }
        }
      if      (
    $out=='file') return $file_list;
      elseif  (
    $out=='dir' ) return $dirs;
      }  
      
    ?>

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.