2011-12-15 96 views
2

FTPを使用してフォルダを削除する機能と、そのフォルダに含まれるすべてのサブフォルダとファイルを追加しようとしています。FTP接続のフォルダとすべてのファイルを削除する

私はこれを行うために再帰的な関数を構築していますが、ロジックが正しいと思っていますが、まだ動作しません。

私はいくつかのテストを行いました。パスが空のフォルダまたはファイルの場合は最初の実行で削除できますが、1つのファイルを含むフォルダまたは1つの空のサブフォルダを含むフォルダの場合は削除できません。だから、フォルダを移動して関数を使って削除することに問題があるようです。

アイデア?

あなたは( ftp_chdirを使用して)確認する必要が
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

は、ディレクトリをするかのいずれかが、あなたがしているサブディレクトリをですスペースを削除しようとしていますか? –

+0

ファイルまたはフォルダのスペースがありません。アルファベットだけです。 – JimmyJammed

+0

問題のディレクトリでファイルシステムのアクセス権を使用すると、ディレクトリを削除できますか? – sarnold

答えて

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_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)に戻り、続行します。

+0

これはちょうど私が持っていた考えでした。しかし、これはまだ動作しません。私はいくつかのテストを行ったので、パスが空のフォルダまたはファイルだけの場合は最初の実行で削除できますが、1つのファイルを含むフォルダまたは1つの空のサブフォルダを含むフォルダの場合は削除できません。ぼんやりした – JimmyJammed

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); 
    } 
} 
+0

うん、これは動作します。私は相対パスでそれを使用しますが、絶対に動かすべきです – vladkras

+0

どのようにftp接続ハンドルを作成しますか?これはcorrentです: '$ conn_id = ftp_connect($ ftp_server); $ handle = ftp_login($ conn_id、$ ftp_user_name、$ ftp_user_pass); ' – Mostafa

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); 
    } 
}