2011-01-30 4 views
1

디렉토리의 파일 내용을 표시하는 기본 PHP 스크립트가 있습니다. 나는 다음 각 파일을 클릭하면 해당 파일이 삭제됩니다에 표시하는 삭제 링크/버튼을 만들려고하고PHP 파일 삭제 스크립트

<?php 

$Dept = "deptTemplate"; 

if(isset($_REQUEST['dir'])) { 
    $current_dir = $_REQUEST['dir']; 
} else { 
    $current_dir = 'docs'; 
} 

if ($handle = opendir($current_dir)) { 
    while (false !== ($file_or_dir = readdir($handle))) { 
     if(in_array($file_or_dir, array('.', '..'))) continue; 
     $path = $current_dir.'/'.$file_or_dir; 
     if(is_file($path)) { 
      echo '`<a href="/Implementation/'.$Dept.'/'.$path.'">'.$file_or_dir.'</a> - [Delete button/link]<br/>`'; 
     } else { 
      echo '`<a href="testFolderiFrame.php?dir='.$path.'">`'.$file_or_dir."\n`</a>` - [Delete button/link]`<br/>`"; 
     } 
    } 
    closedir($handle); 
} 
?> 

다음은 스크립트입니다. 이 작업을 수행하는 방법을 알고 있습니까?

답변

0

나는 그것을 밖으로 일했다 :

- < a href="delete.php?file='.$file_or_dir.'&dir=' . $dir . '"> Delete< /a>< br/>';

이 링크 나이 다운로드 스크립트 페이지로 취

내가이 원래 스크립트에 나열된 각 파일의 끝 부분에 링크를 삭제 추가 , 어떤이처럼 보였다 : 그것은 모두가 지금은 훌륭하게 작동

<?php 
ob_start(); 

$file = $_GET["file"]; 

$getDir = $_GET["dir"]; 

$dir = 'docs/' . $getDir . ''; 

$isFile = ($dir == "") ? 'docs/' . $file . '' : '' . $dir . '/' . $file . ''; 

if (is_file($isFile)){ 
if ($dir == "") 
    unlink('docs/' . $file . ''); 
else 
    unlink('' . $dir . '/' . $file . ''); 

echo '' . $file . ' deleted'; 
echo ' from ' . $dir . ''; 
} 
else{ 
    rmdir('' . $dir . '/' . $file . ''); 
    echo '' . $dir . '/' . $file . ' deleted';} 

header("Location: indexer.php?p=" . $getDir . ""); 
ob_flush(); 

?> 

여러분의 도움과 제안에 대한 여러분 모두 감사합니다 :)

6

내장 된 unlink($filepath) 기능을 사용하십시오.

+3

** 그러나 코드에서 사용자가 해당 파일을 삭제할 수 있음을 확인하는 것을 잊지 마십시오 ** – ThiefMaster

+0

코드에이 함수를 추가 할 위치를 알려주시겠습니까? (나는 PHP에 대해 매우 익숙하지 않다.) – Jopper

0

이와 비슷한? 테스트하지 ...

<?php 

    echo '`<a href="/Implementation/'.$Dept.'/'.$path.'">'.$file_or_dir.'</a> - [<a href=\"?thispage&del=1&file_or_dir=$file_or_dir\">Delete button/link</a>]<br/>`'; 

?> 

<?php 

    if ($_GET['del'] == 1 && isset($_GET['file_or_dir']){ 
     unlink ("path/".$_GET['file_or_dir']); 
    } 

?> 
1

물론, 당신은 unlink()rmdir()를 사용해야 할 것, 그리고 rmdir() 그들에있는 파일과 디렉토리에 작동하지 않기 때문에 당신은 재귀 적 디렉토리 제거 기능을 필요 했어. 또한 모든 사람을 삭제하는 것을 막기 위해 삭제 스크립트가 정말로 안전하도록하고 싶을 수도 있습니다. 재귀 함수에 대한이 같은

뭔가 :

function Remove_Dir($dir) 
{ 
    $error = array(); 
    if(is_dir($dir)) 
    { 
      $files = scandir($dir); //scandir() returns an array of all files/directories in the directory 
      foreach($files as $file) 
      { 
       $fullpath = $dir . "/" . $file; 
       if($file == '..' || $file == '.') 
       { 
        continue; //Skip if ".." or "." 
       } 
       elseif(is_dir($fullpath)) 
       { 
        Remove_Dir($fullpath); //recursively remove nested directories if directory 
       } 
       elseif(is_file($fullpath)) 
       { 
        unlink($fullpath); //Delete file otherwise 
       } 
       else 
       { 
        $error[] = 'Error on ' . $fullpath . '. Not Directory or File.' //Should be impossible error, because everything in a directory should be a file or directory, or . or .., and thus should be covered. 
       } 
      } 

      $files = scandir($dir); //Check directory again 
      if(count($files) > 2) //if $files contains more than . and .. 
      { 
       Remove_Dir($dir); 
      } 
      else 
      { 
       rmdir($dir); //Remove directory once all files/directories are removed from within it. 
      } 
      if(count($error) != 0) 
      {return $error;} 
      else 
      {return true;} 
    } 
} 

그럼 그냥 파일이나 디렉토리를 통과해야 아마 확인, 그것을위한 urlencode() 또는 무언가를 필요로, 스크립트에 GET 또는 뭔가를 삭제할 파일을 삭제하려고 할 때 권한이있는 사용자이고 파일 인 경우 unlink()이고 디렉토리이면 Remove_Dir()입니다.

디렉토리/파일을 제거하기 전에 스크립트의 디렉토리/파일에 전체 경로를 추가해야합니다.

보안을 위해 사용자가 원하는 것은 먼저 삭제가 예정된 장소에서 일어나고 있는지 확인하기 위해 누군가 ?dir=/ 또는 그 밖의 다른 작업을 수행 할 수 없으며 루트에서 전체 파일 시스템을 삭제하려고 시도 할 수 있습니다. 아마도 $dir = '/home/user/public_html/directories/' . $_GET['dir'];과 같은 입력에 적절한 경로를 추가함으로써 우회 될 수 있습니다. 물론 경로에서 모든 것을 잠재적으로 삭제할 수 있습니다. 즉, 사용자가 해당 경로의 권한을 가지고 있는지 확인해야합니다.

경우에 대비하여 파일의 정기적 인 백업을 유지해야합니다.

+0

자세한 답변을 주셔서 감사합니다.하지만 내 코드에이 스크립트를 어디에 둘 것인지 알려주시겠습니까? – Jopper

+0

에 따라 다릅니다. 그것은 당신의 질문에서와 같은 스크립트에서 할 수 있고, delete 매개 변수는 get 매개 변수와 함께 같은 페이지로 가고'if (isset ($ _ GET [ 'del']))'와 같은 것을하면 무언가 삭제가 필요합니다.함수 자체는 다른 함수를 제외하고는 맨 앞에 있어야합니다. 그렇지 않으면 링크 된 별도의 스크립트로 이동합니다. – Phoenix