File deleter

I have many pictures in the folder. Some of them I want to delete. Because
my FTP server shows only 1000 pictures, I am using following script.
Unfortunately it deletes single files only and it takes a time to reload
after deleting.
What I want is your help to modify this script so I would make check marks
at the file and than press a button to delete selected files.
I am sure there are people who would do it without a problem.
Your help will be appreciated.

Andy.

<?php
#########################################
# TheDeleter v1.0 #
# www.magtrb.com #
# www.magtrb.com/en #
# 20/09/06 23:33 #
# #
#########################################

session_start();
$secret = "123"; // Cpanel password
languge(English); //The Language.. English, Arabic ...


####### Don't write any thing below this line ##############
############################################################


if($_GET["logout"]){
session_destroy();
unset ($_SESSION['secret']);
unset ($_SESSION['code']);
echo"<script>
location = \"TheDeleter.php\";
</script>";
}

echo"<html dir=\""._De0."\">
<head>
<title>TheDeleter v1.0</title>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset="._De7."\">";
?>
<script>

function changedirpic(obj)
{
obj.src = "TheDeleter/directoryopen.gif";
}

function changedirpicback(obj)
{
obj.src = "TheDeleter/directory.gif";
}

</script>

<style type="text/css">

FONT {FONT-FAMILY: ms sans serif; FONT-SIZE: 12px}
BODY {FONT-FAMILY: ms sans serif;
FONT-SIZE: 12px;
background:#ffffff}
P {FONT-FAMILY: ms sans serif; FONT-SIZE: 14px}
DIV {FONT-FAMILY: ms sans serif; FONT-SIZE: 14px}

td {

color: #3C0954;
border: 1px solid #4B8CCF;
FONT-FAMILY: ms sans serif; FONT-SIZE: 14px
}
table {
margin-left: auto;
margin-right: auto;
background-color: #9CBFE3;
border: 2px solid #4B8CCF;
border-right: 2px solid #4B8CCF;
border-bottom: 2px solid #4B8CCF;
}
form {
padding: 0px;
margin: 0px;
}
A:link {color:#B63333;
FONT-SIZE: 12px;
FONT-FAMILY: ms sans serif;text-decoration:none}
A:active {color:#B63333;
FONT-SIZE: 12px;
FONT-FAMILY: ms sans serif;text-decoration:none}
A:visited {color:#B63333;
FONT-SIZE: 12px;
FONT-FAMILY: ms sans serif;text-decoration:none}
A:hover {color:#B63333;
FONT-SIZE: 12px;
FONT-FAMILY: ms sans serif;text-decoration:underline}
</style>

</head>

<body>

<br><br><table width="80%" border="0" >
<tr>
<td> <h2 align="center"><img src="TheDeleter/TheDeleter.gif"></h2> </td>
</tr><tr><td><br>

<?php
if($_SESSION['code'] !== $_POST['code'] && $_POST['loginkey']){

$text = $_SESSION['code']= strrand(5);

echo "<p align=\"center\"><b style=\"color: black\">"._De1."</b><br
/></p>
\n";
echo "<form action=\"TheDeleter.php\" method=\"post\"> <p
align=\"center\">\n";
echo ""._De2." :       <span
style=\"background-color:
#CCCCCC\">$text</span>       <input
name=\"code\" type=\"text\" size=\"5\" maxlength=\"5\"><br>\n";
echo ""._De3." : <input name=\"secret\" type=\"password\"
size=\"20\"><br>\n";
echo "<input name=\"loginkey\" type=\"hidden\" value=\"loginkey\">\n";
echo "<input name=\"submit\" type=\"submit\" value=\""._De4."\">\n";
echo "</form><br><br>\n";
exit;

}
if ($_SESSION['secret'] !== $secret) {

if ($_POST['secret'] == $secret) {
$_SESSION['secret'] = $secret;
}
else {
$text = $_SESSION['code']= strrand(5);

echo "<p align=\"center\"><b style=\"color: black\">"._De1."</b><br
/></p>
\n";
echo "<form action=\"TheDeleter.php\" method=\"post\"> <p
align=\"center\">\n";
echo ""._De2." :       <span
style=\"background-color:
#CCCCCC\">$text</span>       <input
name=\"code\" type=\"text\" size=\"5\" maxlength=\"5\"><br>\n";
echo ""._De3." : <input name=\"secret\" type=\"password\"
size=\"20\"><br>\n";
echo "<input name=\"loginkey\" type=\"hidden\" value=\"loginkey\">\n";
echo "<input name=\"submit\" type=\"submit\" value=\""._De4."\">\n";
echo "</form><br><br>\n";
exit;
}

}


if($_GET['submitunlink']){
echo"<table width=\"50%\">
<tr><td bgcolor=\"#C0C0C0\" align=\"center\">";
$dir_file = $_GET['unlink'];

if(str_replace('*', 'x', $dir_file) != $dir_file){

delfile($dir_file);

echo ""._De11."".$dir_file.""._De12."<br>";

}else{

if($dir_file == ''){

echo ""._De13."";

}else{

unlink($dir_file);

echo ""._De11."".$dir_file.""._De12."<br>";

}

}
echo"</td></tr>
</table><br>";
}

if($_GET['submitrmdir']){
echo"<table width=\"50%\" >
<tr><td bgcolor=\"#C0C0C0\" align=\"center\">";
$rm_dir = $_GET['rmdir'];

if($rm_dir == ''){

echo ""._De14."";

}else{

[at] SureRemoveDir($rm_dir);
[at] rm($rm_dir);
deleteDir($rm_dir) ;
echo ""._De15."".$rm_dir.""._De12." <br>";

}
echo"</td></tr>
</table><br>";
}

class displayer
{

var $allowclick = 0;

function checkSecure($directory)
{
if(strstr($directory, ".."))
{
return FALSE;
}

else
{
return TRUE;
}
}

function getPrevious()
{
$directory = $_GET['directory'];

$exdirectory = explode("/", $directory);

for($i=0; $i<count($exdirectory) - 1; $i++)
{
$output .= "/" . $exdirectory[$i];
}

$output = substr($output, 1);

return($output);
}

function displayList()
{

$maindir = $this->maindir;
$directory = $_GET['directory'];

if(!$this->checkSecure($directory))
{
return(""._De17."");
}

$output .= "<table border='0' cellpadding='5' cellspacing='0'>";

$output .= "<tr>";
$output .= "<td colspan='2'><a href='?directory=" . $this->getPrevious() .
"'>"._De18."</a></td>";
$output .= "</tr>";

$count = 0;

$dirname = $_SERVER["DOCUMENT_ROOT"] . $directory;

if(is_dir($_SERVER["DOCUMENT_ROOT"] . $directory))
{
$dir = opendir($dirname);

while(false != ($file = readdir($dir)))
{
$output .= "<tr>";

if(($file != ".") and ($file != ".."))
{
if(is_dir($_SERVER["DOCUMENT_ROOT"] . $directory . "/" . $file))
{
$output .= "<td><a href='?directory=" . $directory . "/" . $file .
"'><img onmouseover='changedirpic(this)' onmouseout='changedirpicback(this)'
border='0' width='16px' height='16px' src='TheDeleter/directory.gif'
/></a></td>";
$output .= "<td>" . $file . "</td>";
if(!empty($directory)){
$directoryxps = substr($directory, 1). "/"; }else{$directoryxps="";}
$output .= "<td><a href=\"TheDeleter.php?submitrmdir=xps&rmdir=" .
$directoryxps . "" . $file . "\">"._De19." $file</a></td>";
}

else
{
if($this->allowclick == 0)
{
$output .= "<td><img border='0' width='16px' height='16px'
src='TheDeleter/file.gif' /></td>";
}

else
{
$output .= "<td><a href='" . $directory . "/" . $file . "'><img
border='0' width='16px' height='16px' src='TheDeleter/file.gif'
/></a></td>";
}

$output .= "<td>" . $file . "</td>";
if(!empty($directory)){
$directoryxps = substr($directory, 1). "/"; }else{$directoryxps="";}

$output .= "<td><a
href=\"TheDeleter.php?submitunlink=xps&unlink=" . $directoryxps . "" . $file
.. "\">"._De19." $file</a></td>";
}

$count++;
}

$output .= "</tr>";
}
}

if($count == 0)
{
$output .= "<tr>";
$output .= "<td colspan='2'>"._De20."</td>";
$output .= "</tr>";
}

$output .= "</table>";

return($output);
}
}

$displayerHandler = new displayer();

$option = $_GET['option'];

if($option == null)
{
$option = "displaylist";
}

switch($option)
{ case "startsession":
echo($displayerHandler->startsession());
break;

case "displaylist":
echo($displayerHandler->displayList());
break;
}


echo"<br><hr width=\"70%\">
<div align=\"center\" style=\"color: #999999; font-size: 7pt;\">
[ <a href=\"TheDeleter.php\">"._De5."</a> ] -[ <a href=\""._De8."\"
target=\"_blank\">"._De9."</a> ]-[ <a
href=\"TheDeleter.php?logout=yes\">"._De10."</a> ]
</div><br>";
echo"</body></html>";





// the functions

function strrand($length)
{
$str = "";

while(strlen($str)<$length){
$random=rand(48,122);
if( ($random>47 && $random<58) ){
$str.=chr($random);
}

}

return $str;
}


function deleteDir($dir)
{
if (substr($dir, strlen($dir)-1, 1) != '/')
$dir .= '/';
// echo $dir;
if ($handle = opendir($dir))
{
while ($obj = readdir($handle))
{
if ($obj != '.' && $obj != '..')
{
if (is_dir($dir.$obj))
{
if (!deleteDir($dir.$obj))
return false;
}
elseif (is_file($dir.$obj))
{
if (!unlink($dir.$obj))
return false;
}
}
}
closedir($handle);
if (! [at] rmdir($dir))
return false;
return true;
}
return false;
}

function rm($fileglob)
{
if (is_string($fileglob)) {
if (is_file($fileglob)) {
return unlink($fileglob);
} else if (is_dir($fileglob)) {
$ok = rm("$fileglob/*");
if (! $ok) {
return false;
}
return rmdir($fileglob);
} else {
$matching = glob($fileglob);
if ($matching === false) {
trigger_error(sprintf('No files match supplied glob %s',
$fileglob), E_USER_WARNING);
return false;
}
$rcs = array_map('rm', $matching);
if (in_array(false, $rcs)) {
return false;
}
}
} else if (is_array($fileglob)) {
$rcs = array_map('rm', $fileglob);
if (in_array(false, $rcs)) {
return false;
}
} else {
trigger_error('Param #1 must be filename or glob pattern, or array of
filenames or glob patterns', E_USER_ERROR);
return false;
}
return true;
}

function SureRemoveDir($dir) {
if(!$dh = [at] opendir($dir)) return;
while (($obj = readdir($dh))) {
if($obj=='.' || $obj=='..') continue;
if (! [at] unlink($dir.'/'.$obj)) {
SureRemoveDir($dir.'/'.$obj);
} else {
$file_deleted++;
}
}
if ( [at] rmdir($dir)) $dir_deleted++;
}

function delfile($str)
{
foreach(glob($str) as $fn) {
[at] unlink($fn);
}
}



function languge($spx){

switch ($spx) {
case English:
define("_De0","ltr");
define("_De1","Administrator Control Panel");
define("_De2","Security code");
define("_De3","Passowrd");
define("_De4","Enter");
define("_De5","Home");
define("_De6","left"); // direction of the text .. left or right
define("_De7","windows-1256");
define("_De8","http://www.magtrb.com/en");
define("_De9","Magtrb Soft");
define("_De10","Exit");
define("_De11","File:");
define("_De12"," was deleted.");
define("_De13","Please choose the file that you would like to delete.");
define("_De14","Please choose the directory that you would like to
delete.");
define("_De15","Directory: ");
define("_De17","Insecure directory.");
define("_De18","Parent Directory");
define("_De19","Delete");
define("_De20","No files or folders found.");

break;
case Arabic:
define("_De0","rtl");
define("_De1","لوحة التحكم");
define("_De2","الكود الامني");
define("_De3","الرقم السري");
define("_De4","دخول");
define("_De5","الرئيسية");
define("_De6","right"); // direction of the text ..
define("_De7","windows-1256");
define("_De8","http://www.magtrb.com");
define("_De9","مغترب");
define("_De10","خروج");
define("_De11","الملف:");
define("_De12","تم حذفه");
define("_De13","الرجاء اختيار الملف المراد حذفه.");
define("_De14","الرجاء اختيار المجلد المراد حذفه.");
define("_De15","المجلد: ");
define("_De17","مجلد غير محمي.");
define("_De18","المجلد الرئيسي");
define("_De19","حذف");
define("_De20","لا يوجد ملفات او مجلدات.");
break;
case xxx:
break;
}



}

?>
Andy [ So, 04 Februar 2007 02:00 ] [ ID #1618316 ]

Re: File deleter

Andy schreef:
> I have many pictures in the folder. Some of them I want to delete. Because
> my FTP server shows only 1000 pictures, I am using following script.
> Unfortunately it deletes single files only and it takes a time to reload
> after deleting.
> What I want is your help to modify this script so I would make check marks
> at the file and than press a button to delete selected files.
> I am sure there are people who would do it without a problem.
> Your help will be appreciated.

Presuming the files have no strange characters. Otherwise use
urlencode($file) iirc. I didn't test this btw so be carefull (use echo
"unlink ('/path/to/file/'$file)"; first

if (isset($_POST))
{
foreach ($_POST['file'] as $myfile)
{
unlink('/path/to/file'.$file);
}
}

echo '<form>'; // etc
$handle=opendir($path);
while (false!==($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo '<input type="checkbox" name="file[]"
value="'.$file.'"/>'.$file.'<br/>
}
}
closedir($handle);
echo '<input type = "submit">';//etc

--
Arjen
http://www.hondenpage.com
Dave Williams [ So, 04 Februar 2007 21:28 ] [ ID #1618319 ]

Re: File deleter

"Arjen" <dont [at] mail.me> wrote in message
news:45c641f9$0$64626$dbd4d001 [at] news.wanadoo.nl...
> Andy schreef:
>> I have many pictures in the folder. Some of them I want to delete.
>> Because my FTP server shows only 1000 pictures, I am using following
>> script. Unfortunately it deletes single files only and it takes a time to
>> reload after deleting.
>> What I want is your help to modify this script so I would make check
>> marks at the file and than press a button to delete selected files.
>> I am sure there are people who would do it without a problem.
>> Your help will be appreciated.
>
> Presuming the files have no strange characters. Otherwise use
> urlencode($file) iirc. I didn't test this btw so be carefull (use echo
> "unlink ('/path/to/file/'$file)"; first
>
> if (isset($_POST))
> {
> foreach ($_POST['file'] as $myfile)
> {
> unlink('/path/to/file'.$file);
> }
> }
>
> echo '<form>'; // etc
> $handle=opendir($path);
> while (false!==($file = readdir($handle))) {
> if ($file != "." && $file != "..") {
> echo '<input type="checkbox" name="file[]"
> value="'.$file.'"/>'.$file.'<br/>
> }
> }
> closedir($handle);
> echo '<input type = "submit">';//etc
>
> --
> Arjen
> http://www.hondenpage.com
>

I tried to follow your message but I get a parse error.
I am a new to PHP and do not know how to modify my script.
In order to test it place it in any directory on your site. It is really
nice deleter, but I need multiple file deleter with check boxes.
Andy.
Andy [ Mo, 05 Februar 2007 01:07 ] [ ID #1619376 ]

Re: File deleter

Message-ID: <eq5sfs$rfj$1 [at] aioe.org> from Andy contained the following:

>I tried to follow your message but I get a parse error.
>I am a new to PHP and do not know how to modify my script.
>In order to test it place it in any directory on your site. It is really
>nice deleter, but I need multiple file deleter with check boxes.

Cheap and cheerful. Save this lot as deleter.php and run it from the
directory you want to control


<?php
$notify="";
?>
<HTML>
<HEAD>
<TITLE>File management</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<?php
if(isset($_POST['del'])){
$notify="<br> ".count($_POST['delete'])." file(s) deleted";}
echo "<center><em style = \"font-weight:
bold;\">".$message.$notify."</em></center>";


?>
<FORM style="border 1px solid black; background-color:#9999ff;
padding:20px;" ENCTYPE="multipart/form-data" name="form" ACTION="<?php
echo $_SERVER['PHP_SELF']?>" METHOD="POST">
<table align="center"width="100%" border="0" cellspacing="0"
cellpadding="0">
<?php
if(isset($_POST['del']))
{$j=0;
$error="";
for($i=0;$i<count($_POST['delete']);$i++){
if(! [at] unlink($path.$_POST['delete'][$i])){
$j++;
$error="<b><p>$j file(s) could not be deleted</b></p>";}
}
print $error;
}
?>
</P>
</td>
</tr>
</table>


<table align="center" border="1" cellspacing="0" cellpadding="2">
<?php
if(empty($_POST['cdir'])){
//$dir = $path."";
$path=$_SERVER['PATH_TRANSLATED'];
//print "$path<br>";
$i= strlen($path);
//print"$i<br>";
do{$i--;}
while(strpos($path,"/",$i)===false);
//print"$i<br>";
//print substr($path,0,$i);
$path=substr($path,0,$i+1);
$dir=$path;
}
else{
$dir=$_SERVER['DOCUMENT_ROOT'].$_POST['cdir'];
}
// Open a known directory, and proceed to read its contents
//if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
//print $dir . $file;
if(($file!="."&&$file!="..")){
echo "<tr><td>filename: </td><td><b> <a
href=\"$file\">$file</a></b></td><td>Type: </td><td>". filetype($dir
.. $file)."</td><td align=\"center\"><input
type=\"checkbox\"name=\"delete[]\" value=\"$file\"><td></tr>\n";
}
}
closedir($dh);
}
//}
?>
<tr>
<td colspan=5 align="right"><input type="submit" value ="Delete
selected" name="del"><td></tr>
<tr><td colspan="5" align="left">
<!--<input type="textbox"name="cdir"> <input type="submit" value
="change dir" name="cd">-->
<td></tr>

</table>
</form>
</BODY>
</HTML>


--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
Geoff Berrow [ Mo, 05 Februar 2007 02:02 ] [ ID #1619377 ]

Re: File deleter

"Geoff Berrow" <blthecat [at] ckdog.co.uk> wrote in message
news:ea0ds2ppq61cjlptokkml7cnjrtsv09352 [at] 4ax.com...
> Message-ID: <eq5sfs$rfj$1 [at] aioe.org> from Andy contained the following:
>
>>I tried to follow your message but I get a parse error.
>>I am a new to PHP and do not know how to modify my script.
>>In order to test it place it in any directory on your site. It is really
>>nice deleter, but I need multiple file deleter with check boxes.
>
> Cheap and cheerful. Save this lot as deleter.php and run it from the
> directory you want to control
>

Thank you very much Geoff. It works.
Best Regards.
Andy
Andy [ Mo, 05 Februar 2007 12:26 ] [ ID #1619386 ]

Re: File deleter

Message-ID: <eq749o$fjo$1 [at] aioe.org> from Andy contained the following:

>> Cheap and cheerful. Save this lot as deleter.php and run it from the
>> directory you want to control
>>
>
>Thank you very much Geoff. It works.

Don't leave it on there else anyone could delete your files. :-)

--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
Geoff Berrow [ Mo, 05 Februar 2007 13:02 ] [ ID #1619387 ]

Re: File deleter

"Geoff Berrow" <blthecat [at] ckdog.co.uk> wrote in message
news:n07es2hmd4dgbom8dac9b2kpfpoumtpli6 [at] 4ax.com...
> Message-ID: <eq749o$fjo$1 [at] aioe.org> from Andy contained the following:
>
>>> Cheap and cheerful. Save this lot as deleter.php and run it from the
>>> directory you want to control
>>>
>>
>>Thank you very much Geoff. It works.
>
> Don't leave it on there else anyone could delete your files. :-)
>
OK. I will not.
Thanks.
Andy
Andy [ Mo, 05 Februar 2007 20:22 ] [ ID #1619390 ]
PHP » alt.php » File deleter

Vorheriges Thema: looking for a programmer that know fckeditor
Nächstes Thema: how to choose the correct height/width for images