PHP删除文件脚本
我有一个显示目录文件内容的基本PHP脚本。以下是脚本:PHP删除文件脚本,php,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
<?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);
}
?>
使用内置函数。使用内置函数。像这样吗?未经测试
<?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']);
}
?>
像这样的?未经测试
<?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']);
}
?>
当然,您必须使用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\u Dir()
如果它是一个目录
在删除目录/文件之前,您必须在脚本中将目录或文件的完整路径前置到目录/文件
为了安全起见,您需要做的一些事情是,首先确保删除发生在它应该发生的位置,这样某人就不能执行?dir=/
或其他操作,并尝试从根目录中删除整个文件系统,这可能可以通过在输入前加上适当的路径来避免,比如$dir='/home/user/public_html/directories/'$_获取['dir']当然,他们可以删除该路径中的所有内容,这意味着您需要确保用户有权这样做
需要定期备份文件以备不时之需。当然,您必须使用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\u Dir()
如果它是一个目录
在删除目录/文件之前,您必须在脚本中将目录或文件的完整路径前置到目录/文件
为了安全起见,您需要做的一些事情是,首先确保删除发生在它应该发生的位置,这样某人就不能执行?dir=/
或其他操作,并尝试从根目录中删除整个文件系统,这可能可以通过在输入前加上适当的路径来避免,比如$dir='/home/user/public_html/directories/'$_获取['dir']当然,他们可以删除该路径中的所有内容,这意味着您需要确保用户有权这样做
需要定期备份文件以防万一。我已经解决了:
我在原始脚本中列出的每个文件的末尾添加了此删除链接:
-delete
代码>
此链接将我带到下载脚本页面,如下所示:
<?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();
?>
现在一切都很好,谢谢大家的帮助和建议:)我已经解决了:
我在原始脚本中列出的每个文件的末尾添加了此删除链接:
-delete
代码>
此链接将我带到下载脚本页面,如下所示:
<?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();
?>
现在一切都很好,谢谢大家的帮助和建议:)但别忘了在代码中签入允许用户删除该文件的权限。您能告诉我在代码中的何处添加此功能吗?(我对php不是很有经验)但别忘了在代码中检查是否允许用户删除该文件。您能告诉我在代码中的何处添加此函数吗?(我对php不是很有经验)谢谢你的详细回答,但是你能告诉我在我的代码中把这个脚本放在哪里吗。它可以在与您的问题相同的脚本中完成,删除链接将转到带有get参数的同一页面,您可以执行类似于if(isset($\u get['del'))
的操作来确定是否需要删除某些内容。函数本身应该在除其他函数之外的任何函数之前接近顶部。否则,它将进入一个单独的脚本中,get链接到。感谢您提供详细的答案,但您能否告诉我在我的代码中放置此脚本的位置?具体取决于。它可以在与您的问题相同的脚本中完成,删除链接将转到带有get参数的同一页面,您可以执行类似于if(isset($\u get['del'))
的操作来确定是否需要删除某些内容。函数本身应该在除其他函数之外的任何函数之前接近顶部。否则,它将在get链接到的单独脚本中运行。