Форум: Форум PHPФорум ApacheФорум Регулярные ВыраженияФорум MySQLHTML+CSS+JavaScriptФорум FlashРазное
Новые темы: 0000000
Самоучитель MySQL 5. Авторы: Кузнецов М.В., Симдянов И.В. Самоучитель PHP 5 / 6 (3 издание). Авторы: Кузнецов М.В., Симдянов И.В. Социальная инженерия и социальные хакеры. Авторы: Кузнецов М.В., Симдянов И.В. Объектно-ориентированное программирование на PHP. Авторы: Кузнецов М.В., Симдянов И.В. Программирование. Ступени успешной карьеры. Авторы: Кузнецов М.В., Симдянов И.В.
ВСЕ НАШИ КНИГИ
Консультационный центр SoftTime

Форум PHP

Выбрать другой форум

 

Здравствуйте, Посетитель!

вид форума:
Линейный форум Структурный форум

тема: Удаление не пустой папки(директории) с FTP сервера
 
 автор: OD   (19.08.2008 в 16:38)   письмо автору
 
 

Удаление не пустой папки(директории) с FTP сервера
Нашёл такой код, но он работает некоректно, неудаляються файлы во вложенных папках(директориях) т.е
работют варианты:
dir. dir
dir. file
не работют варианты:
dir. dir.file
dir. dir.dir.file
и т.д.
Подскажите пожалуйста как можно решить эту проблемму?

function ftp_rmdirr($ftp_stream, $directory)
{
    // Sanity check
    if (!is_resource($ftp_stream) ||
        get_resource_type($ftp_stream) !== 'FTP Buffer') {

        return false;
    }

    // Init
    $i             = 0;
    $files         = array();
    $folders       = array();
    $statusnext    = false;
    $currentfolder = $directory;

    // Get raw file listing
    $list = ftp_rawlist($ftp_stream, $directory, true);

    // Iterate listing
    foreach ($list as $current) {

        // An empty element means the next element will be the new folder
        if (empty($current)) {
            $statusnext = true;
            continue;
        }

        // Save the current folder
        if ($statusnext === true) {
            $currentfolder = substr($current, 0, -1);
            $statusnext = false;
            continue;
        }

        // Split the data into chunks
        $split = preg_split('[ ]', $current, 9, PREG_SPLIT_NO_EMPTY);
        $entry = $split[8];
        $isdir = ($split[0]{0} === 'd') ? true : false;

        // Skip pointers
        if ($entry === '.' || $entry === '..') {
            continue;
        }

        // Build the file and folder list
        if ($isdir === true) {
            $folders[] = $currentfolder . '/' . $entry;
        } else {
            $files[] = $currentfolder . '/' . $entry;
        }

    }

    // Delete all the files
    foreach ($files as $file) {
        ftp_delete($ftp_stream, $file);
    }

    // Delete all the directories
    // Reverse sort the folders so the deepest directories are unset first
    rsort($folders);
    foreach ($folders as $folder) {
        ftp_rmdir($ftp_stream, $folder);
    }

    // Delete the final folder and return its status
    return ftp_rmdir($ftp_stream, $directory);
}


ftp_rmdirr($ftp_stream, $directory);

  Ответить  
 
 автор: coba   (19.08.2008 в 19:58)   письмо автору
 
   для: OD   (19.08.2008 в 16:38)
 

Предлагаю свой код, который удаляет файлы старше определенного периода и удаляет пустые директории после удаления файлов.
При этом он пропускает директории, определенные в excude.dir
Начинает работу с каталога $start_dir
<?php

function FileSearch($StartPath,$doNotInsArr)
{
 
$handle=opendir($StartPath);
 
$i_lenarr=count($doNotInsArr);
 while (
false!==($file=readdir($handle)))
 {
  if ((
$file!='.')&&($file!='..'))
  {
   if (
is_dir($StartPath."\\".$file))
   {
    
$ins=true;
    
$fdir=$StartPath."\\".$file;
    if (
$i_lenarr>0)
    {
     for (
$i=0;$i<$i_lenarr;$i++)
     {
      
$pos=strpos($fdir,$doNotInsArr[$i]);
      if (
$pos!==false)
      {
       
$ins=false;
       break;
      }
     }
    }
    if (
$ins===true)
    {
     
FileSearch($fdir,$doNotInsArr);
    }
   }
   else
   {
    
$fname=$StartPath."\\".$file;
    if ((
time()-filemtime($fname))>1296000)
    {
     if (
unlink($fname))
     {
      
$flog=fopen('delete_files.log','a');
      
fwrite($flog,date("d.m.Y h:i:s").' - '.$fname."\n");
      
fclose($flog);
     }
    }
   }
  }
 }
 
closedir($handle);
 if (
rmdir($StartPath))
 {
  
$flog=fopen('delete_files.log','a');
  
fwrite($flog,date("d.m.Y h:i:s").' - '.$StartPath."\n");
  
fclose($flog);    
 }
}

set_time_limit(0);
$arr=array();
$arr=file('exclude.dir');
$len=count($arr);
if (
$len>0)
{
 for (
$i=0;$i<$len;$i++)
 {
  
$arr[$i]=rtrim($arr[$i]);
 }
}
FileSearch($start_dir,$arr);
?>

  Ответить  
 
 автор: xx77   (19.08.2008 в 20:14)   письмо автору
 
   для: coba   (19.08.2008 в 19:58)
 

<?
function ftp_dd($link$dir)

$list ftp_nlist($link$dir); 
$jcou count($list);
if (
$jcou 2) { 
for(
$j 0$j $jcou$j++) 
{
$fil $list[($j)];
if ((
$fil == '.') or ($fil == '..')) continue;
$fl $dir '/' $fil ;
if (
ftp_size($link$fl) != -1) { 
@
ftp_delete($link$fl);
} else {
ftp_dd($link$fl);
} } }
if(@
ftp_rmdir($link$dir))
return 
;
else
return 
0;
}

местами работало

  Ответить  
Rambler's Top100
вверх

Rambler's Top100 Яндекс.Метрика Яндекс цитирования