2011-01-30 114 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 2011-01-30 17:45:17

+0

你能告訴我在代碼中添加這個函數的位置嗎? (我對php的使用經驗不多) – Jopper 2011-01-30 18:09:12

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

然後你只需要通過文件或目錄通過GET或東西劇本中刪除,這可能需要urlencode()或東西是,確保它是一個授權用戶,有權刪除試圖刪除的東西,如果是文件則爲unlink(),如果是目錄,則爲Remove_Dir()

在刪除目錄/文件之前,您必須預先將目錄或文件的完整路徑預先加載到腳本中的目錄/文件中。

你需要安全的一些事情首先確保刪除發生在它應該的位置,所以有人不能做?dir=/或其他東西,並嘗試從根目錄刪除整個文件系統,它可以可能會通過將諸如$dir = '/home/user/public_html/directories/' . $_GET['dir'];之類的適當路徑添加到輸入上來繞開,當然那麼它們可能會刪除該路徑中的所有內容,這意味着您需要確保用戶有權這樣做。

爲了以防萬一需要定期備份文件。