2011-12-15 93 views
2

嘗試添加使用FTP和該文件夾中包含的所有子文件夾和文件刪除文件夾的功能。刪除FTP連接中的文件夾和所有文件

我已經構建了一個遞歸函數來做到這一點,我覺得這個邏輯是正確的,但仍然無法正常工作。

我做了一些測試,我可以在第一次運行時刪除,如果路徑只是一個空文件夾或只是一個文件,但不能刪除,如果它是包含一個文件的文件夾或包含一個空子文件夾的文件夾。所以它似乎是遍歷文件夾並使用函數刪除的問題。

任何想法?

function ftpDelete($directory) 
{ 
    if(empty($directory))//Validate that a directory was sent, otherwise will delete ALL files/folders 
     return json_encode(false); 
    else{ 
     global $conn_id; 
     # here we attempt to delete the file/directory 
     if(!(@ftp_rmdir($conn_id,$directory) || @ftp_delete($conn_id,$directory))) 
     { 
      # if the attempt to delete fails, get the file listing 
      $filelist = @ftp_nlist($conn_id, $directory); 

      # loop through the file list and recursively delete the FILE in the list 
      foreach($filelist as $file) 
       ftpDelete($file); 

      #if the file list is empty, delete the DIRECTORY we passed 
      ftpDelete($directory); 
     } 
     else 
      return json_encode(true); 
    } 
}; 
+0

不目錄或任何的那就是你的子目錄試圖刪除包含空格? – 2011-12-15 00:49:43

+0

文件或文件夾不能有空格。只是字母字符。 – JimmyJammed 2011-12-15 00:59:22

+0

對相關目錄的文件系統權限是否允許刪除目錄? – sarnold 2011-12-15 01:49:43

回答

1

好的發現我的問題。由於我沒有移動到精確的目錄,我試圖刪除,每個遞歸文件的路徑被稱爲是不是絕對的:

function ftpDeleteDirectory($directory) 
{ 
    global $conn_id; 
    if(empty($directory))//Validate that a directory was sent, otherwise will delete ALL files/folders 
     return json_encode(false); 
    else{ 
     # here we attempt to delete the file/directory 
     if(!(@ftp_rmdir($conn_id,$directory) || @ftp_delete($conn_id,$directory))) 
     { 
      # if the attempt to delete fails, get the file listing 
      $filelist = @ftp_nlist($conn_id, $directory); 
      # loop through the file list and recursively delete the FILE in the list 
      foreach($filelist as $file) 
      { 
      // return json_encode($filelist); 
       ftpDeleteDirectory($directory.'/'.$file);/***THIS IS WHERE I MUST RESEND ABSOLUTE PATH TO FILE***/ 
      } 

      #if the file list is empty, delete the DIRECTORY we passed 
      ftpDeleteDirectory($directory); 
     } 
    } 
    return json_encode(true); 
}; 
0

你必須檢查(使用ftp_chdir)爲每一個「文件」你ftp_nlist去檢查它是否是一個目錄:

foreach($filelist as $file) 
{ 
    $inDir = @ftp_chdir($conn_id, $file); 

    ftpDelete($file) 

    if ($inDir) @ftp_cdup($conn_id); 
} 

這容易招會的工作,因爲如果ftp_chdir作品,目前的$file實際上是一個文件夾,你已經進入了它。然後遞歸調用ftpDelete,讓它刪除該文件夾中的文件。之後,您移回(ftp_cdup)繼續。

3

我花了一些時間用ftp編寫我自己的遞歸刪除函數版本,這個應該是完全可用的(我自己測試過)。

試一試並修改它以適合您的需求,如果它仍然無法正常工作還有其他問題。你有沒有檢查你想刪除的文件的權限?

function ftp_rdel ($handle, $path) { 

    if (@ftp_delete ($handle, $path) === false) { 

    if ($children = @ftp_nlist ($handle, $path)) { 
     foreach ($children as $p) 
     ftp_rdel ($handle, $p); 
    } 

    @ftp_rmdir ($handle, $path); 
    } 
} 
1
function recursiveDelete($handle, $directory) 
{ echo $handle; 
    # here we attempt to delete the file/directory 
    if(!(@ftp_rmdir($handle, $directory) || @ftp_delete($handle, $directory))) 
    {    
     # if the attempt to delete fails, get the file listing 
     $filelist = @ftp_nlist($handle, $directory); 
     // var_dump($filelist);exit; 
     # loop through the file list and recursively delete the FILE in the list 
     foreach($filelist as $file) {    
      recursiveDelete($handle, $file);    
     } 
     recursiveDelete($handle, $directory); 
    } 
} 
相關問題