2011-01-30 200 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'];之类的适当路径添加到输入上来绕开,当然那么它们可能会删除该路径中的所有内容,这意味着您需要确保用户有权这样做。

为了以防万一需要定期备份文件。