2011-12-15 6 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

디렉토리를 수행하거나의 당신은 하위 디렉토리입니다 공백을 삭제하려고합니까? –

+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_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)하면 계속 진행됩니다.

+0

이것은 내가 가지고 있었던 생각뿐이었습니다. 그러나 이것은 아직도 작동하지 않습니다. 몇 가지 테스트를 했으므로 경로가 빈 폴더 또는 파일 일 경우 처음 실행할 때 삭제할 수 있지만 하나의 파일이 포함 된 폴더 또는 하나의 빈 하위 폴더가 포함 된 폴더 인 경우 삭제할 수 없습니다. 덜컹 거리다 – 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); 
    } 
} 
관련 문제