Главная | Контакты | Настройки СМЕНИТЬ ПАЛИТРУ:

Главная > Программирование > PHP

Рекурсивное чтение каталогов

Пример 1

function read_folder($folder)
{
    $files = scandir($folder);
    foreach($files as $file)
    {
        if ($file == '.' || $file == '..') continue;
        $fullfilename = $folder . '/' . $file;
        echo $fullfilename . '<br>';
        if (is_dir($fullfilename)) 
            read_folder($fullfilename);
    }
}

read_folder('./myfolder');

Пример 2

     <?php
     
     function dir_path($dir) {
     $dh = opendir($dir);
       $path_curent = '';
       while (($file = readdir($dh)) !== false)
         if ($file != "." and $file != "..")
           {
             $path = $dir."/".$file;
             if (is_dir($path))
               {
                 $path_curent .= "$path"."<br>";
                 $path_curent .= dir_path($path);
               }
     /* можно и файлы
             else
               {
                 if (is_file($path))
                   {
                   $path_curent .= "$path"."<br>";
                   }
               }
     */
              
           }
       closedir($dh);
       return $path_curent;
     }
     echo dir_path('./smarty');
     
     ?>

Функция копирования директории со всеми вложениями

<?php

function
dircpy($basePath, $source, $dest, $overwrite = false)
{

if(!is_dir($basePath . $dest))mkdir($basePath . $dest);

if($handle = opendir($basePath . $source))
{

while(false !== ($file = readdir($handle)))
{

if($file != '.' && $file != '..')
{
$path = $source . '/' . $file;

if(is_file($basePath . $path))
{

if(!is_file($basePath . $dest . '/' . $file) || $overwrite)
if(!@copy($basePath . $path, $basePath . $dest . '/' . $file))
{
echo '<font color="red">File (' . $path . ') could not be copied, likely a permissions problem.</font>';
}
}

elseif(is_dir($basePath . $path))
{

if(!is_dir($basePath . $dest . '/' . $file))mkdir($basePath . $dest . '/' . $file);
dircpy($basePath, $path, $dest . '/' . $file, $overwrite);
}
}
}

closedir($handle);
}
}

Главная > Программирование > PHP