2013-06-12 2 views
-1

В моем сервере у меня есть папки и подкаталогКак придавить папку в PHP

Я хочу, чтобы вызвать Свести METHODE в каталог, чтобы переместить каждые файлы на том же уровне, и удалить все пустые папки

Вот что я сделал до сих пор:

public function flattenDir($dir, $destination=null) { 
     $files = $this->find($dir . '/*'); 
     foreach ($files as $file) { 
      if(!$destination){ 
       $destination = $dir . '/' . basename($file); 
      } 
      if (!$this->isDirectory($file)) { 
       $this->move($file, $destination); 
      }else{ 
       $this->flattenDir($file, $destination); 
      } 
     } 
     foreach ($files as $file) { 
      $localdir = dirname($file); 
      if ($this->isDirectory($localdir) && $this->isEmptyDirectory($localdir) && ($localdir != $dir)) { 
       $this->remove($localdir); 
      } 
     } 
    } 


public function find($pattern, $flags = 0) { 

     $files = glob($pattern, $flags); 

     foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) { 
      $files = array_merge($files, $this->find($dir . '/' . basename($pattern), $flags)); 
     } 

     return $files; 
    } 

Этот код не показывают какую-либо ошибку на ходу, но resukt не как ожидалось.

Для Exemple, если у меня есть /folder1/folder2/file я хочу иметь /folder2/file как результат, но вот папки по-прежнему, как они где ...

+0

Там не кажется, вопрос? Ваш общий подход, похоже, находится на пути. –

+0

посмотрите http://stackoverflow.com/questions/1833518/remove-empty-subfolders-with-php и http://stackoverflow.com/questions/2398147/php-recursive-directory-path –

+0

Имеете ли вы проблема с вашим скриптом? Любые ошибки? – chrislondon

ответ

0

я, наконец, сделать мой код работает, я его здесь, в случае, это поможет кому-то

Я упростил его, чтобы сделать его более эффективным. Кстати эта функция является частью FileManager Класса я сделал, поэтому я использую функцию моего собственного класса, но вы можете просто заменить $this->move($file, $destination); на move($file, $destination);

public function flattenDir($dir, $destination = null) { 
      $files = $this->find($dir . '/*'); 
      foreach ($files as $file) { 
       $localdir = dirname($file); 

       if (!$this->isDirectory($file)) { 
        $destination = $dir . '/' . basename($file); 
        $this->move($file, $destination); 
       } 
       if ($this->isDirectory($localdir) && $this->isEmptyDirectory($localdir) && ($localdir != $dir)) { 
        $this->remove($localdir); 
       } 
      } 
    } 
Смежные вопросы