• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

[PHP] Simple file deletion script?

Anayet

New Member
Is it possible to use a PHP page, so that it can be used to delete files from the folder its put in?

I.e. select the files, and then just select delete?

I have a specific folder at the moment, and i stores files temporarily to share with people, but every time i want to delete them, i have to ftp and delete.

Is it possible to have a PHP page that i could do this though?

If so, anyone know a good script? I just want it to be very very simple, and basic, nothing extravagent :)


Thanks in advance
:biggrin2:
 
Anybody know whether this directory listing script can be modified, so that a simple link next to each normal link appears - which would allow me to delete that specific file?

PHP:
<?php 
if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle))) { 
        if ($file != "." && $file != ".." && $file != "style.css" && $file != "index.php" && $file != "indexer.php") { 
            echo "<a href='$file'>$file</a><br>"; 
        } 
    }
    closedir($handle); 
}
?>

Or is it not possible?

Thanks again
 
Originally posted by Anayet
Anybody know whether this directory listing script can be modified, so that a simple link next to each normal link appears - which would allow me to delete that specific file?

PHP:
<?php 
if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle))) { 
        if ($file != "." && $file != ".." && $file != "style.css" && $file != "index.php" && $file != "indexer.php") { 
            echo "<a href='$file'>$file</a><br>"; 
        } 
    }
    closedir($handle); 
}
?>

Or is it not possible?

Thanks again

it's possible, fairly easy actually. but i havent been using php for awhile now so im going to have to look up the commands. will post when i have it.
 
i figured it out, but i can't get it to work properly:(

index.php
Code:
<?php 


if ($handle = opendir('.')) {

    while (false !== ($file = readdir($handle))) { 

        if ($file != "." && $file != ".." && $file != "style.css" && $file != "index.php" && $file != "indexer.php") { 

            echo "<a href='$file'>$file</a> | ";
	    echo "<a href='delete.php?del=$file'>Delete</a><br>";

        } 

    }

    closedir($handle); 

}



?>

delete.php
Code:
<?php

$file = 

if($del==$file) {

  unlink("$file");

} else {

  print "Sorry, you cannot access this file directly";

}

?>

i can't get it use $file as anything. any ideas on how to fix this ?
 
<?php

if (file_exists($file)) {
if(unlink($file)) echo "File Successfully Deleted";
else echo "Error: File unable to be deleted";
}
else echo "Error: file does not exist";
}
?>
 
-------- browser.php
PHP:
<?
$del = $_GET['del'];
if(!$del) {

if ($handle = opendir('.')) {

    while (false !== ($file = readdir($handle))) {

        if ($file != "." && $file != ".." && $file != "style.css" && $file != "index.php" && $file != "indexer.php") {

            echo "<a href='$file'>$file</a> | ";
            echo "<a href='browser.php?del=$file'>Delete</a><br>";

        }

    }

    closedir($handle);

}

}

else {
if (file_exists($del)) {
      if(unlink($del)) echo "File Successfully Deleted";
      else echo "Error: File unable to be deleted";
}
else echo "Error: file does not exist";
}
?>
 
Last edited:
Sorry to bother you again, but i now keep on getting this message: Error: file does not exist

And doesn't delete the file :(
 
Last edited:
This code is not secure, because a user may enter full path (/home/www/digital/files/index.php) or
use ../../file.ext , so I recommand this one :
browser.php :
PHP:
<?php 
if(isset($HTTP_GET_VARS['del'])) { $h = opendir('.');
 while (($file = readdir($h)) !== false) 
  if($file != "." && $file != ".." && is_file($file) && $file==$HTTP_GET_VARS['del'] && $file != "style.css" && $file != "index.php" && $file != "indexer.php") 
  { unlink($file) ;
    die("$file Deleted .") ; }
 echo "Can not delete the file ." ; }
else if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle))) { 
        if ($file != "." && $file != ".." && $file != "style.css" && $file != "index.php" && $file != "indexer.php") { 
            if(is_file($file)) echo "<a href=$file>$file</a> (<a href=browser.php?del=$file>Delete</a>)<br>"; 
        } 
    }
    closedir($handle); 
 }
?>
 
Thank you so much everyone, this is going save me so much time :D


Thanks again, appreciate the great help :)
 
Would it be possible to edit these files so that they can output "file size/kb" & "last modified date" aswell as the filename itself?
Also, would it be possible to have a delete confirmation of some sort?

:confused2 :classic2:
 
PHP:
<?php 
if(isset($HTTP_GET_VARS['del'])) { $h = opendir('.');
 while (($file = readdir($h)) !== false) 
  if($file != "." && $file != ".." && is_file($file) && $file==$HTTP_GET_VARS['del'] && $file != "style.css" && $file != "index.php" && $file != "indexer.php") 
  { if(isset($HTTP_GET_VARS['sure'])) unlink($file) ;
    else die('Delete '. $file .' ? <a href=browser.php?del='.$file.'&sure>Yes</a> <a href=browser.php>No</a>') ;
    die("$file Deleted .<br><a href=browser.php>Browser</a>") ; }
 echo "Can not delete the file ." ; }
else if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle))) { 
        if ($file != "." && $file != ".." && $file != "style.css" && $file != "index.php" && $file != "indexer.php") { 
            if(is_file($file)) echo "<a href=$file>$file</a> <font color=green>Last Modified: ". date("m-d-Y h:i A", filemtime($file)). "</font> <font color=brown>Size: " . filesize($file) . "</font> ( <a href=browser.php?del=$file>Delete</a> )<br>"; 
        } 
    }
    closedir($handle); 
 }
?>
 
Here's one I made for you, has a little authorisation in it too, lets you delete multiple files at once, and prints the last time the file was accessed and last time it was modified.

index.php
PHP:
<?php
//------------------------------------------------------------------------->
// Change the below 2 values to the username and password you want to have
//------------------------------------------------------------------------->
$setting_username = "admin";
$setting_password = "pass";
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

session_start();

echo <<< END
<html>
<head>
    <title>Log In</title>
    <style type="text/css">
    <!--
        body, td { font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;  font-size: x-small;  color: Black;   }
        button { background-color: #EEEEEE;  border: 1px ridge Gray;  font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;  font-size: xx-small;  font-weight: bold;     }
        input { background-color: #EEEEEE;  border: 1px solid Black;  font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;  font-size: xx-small;  font-weight: bold;     }
    -->
    </style>
</head>
<body>
END;

if(!isset($_POST['pass'])) {
    echo "<table cellspacing=0 cellpadding=0 width=\"100%\" height=\"100%\">";
    echo "<tr><td align=\"center\" valign=\"center\">";

    echo "<h3>Log In</h3>";
    echo "<form action=\"$PHP_SELF\" method=\"post\">";
    echo "<table>";
    echo "<tr><td>User: </td><td><input type=\"text\" name=\"user\" size=25></td></tr>";
    echo "<tr><td>Pass: </td><td><input type=\"password\" name=\"pass\" size=25></td></tr>";
    echo "<tr><td colspan=2><div align=\"right\"><input type=\"submit\" value=\"Log In »\"></div></td></tr>";
    echo "</table>";
    echo "</form>";

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

else    {
    echo "<table cellspacing=0 cellpadding=0 width=\"100%\" height=\"100%\">";
    echo "<tr><td align=\"center\" valign=\"center\">";

    if(($_POST['user'] == $setting_username) && ($_POST['pass'] == $setting_password)) {

            echo "<h3>Successful</h3>";
            echo "<table width=400>";
            echo "<tr><td>User name and password is correct.</td></tr>";
            echo "<tr><td><div align=\"right\"><input type=\"button\" value=\"Continue »\" onClick=\"window.location='main.php'\"></div></td></tr>";
            echo "</table>";
            session_register("isokay");
            $isokay = 1;
    }

    else    {
        echo "<h3>Invalid</h3>";
        echo "<table width=400>";
        echo "<tr><td>Incorrect log in.</td></tr>";
        echo "<tr><td><div align=\"right\"><input type=\"button\" value=\"Try Again »\" onClick=\"window.location='$PHP_SELF'\"></div></td></tr>";
        echo "</table>";
    }

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

echo <<< END
</body>
</html>
END;

?>

main.php
PHP:
<?php

session_start();

echo <<< END
<html>
<head>
    <title>Select Files</title>
    <style type="text/css">
    <!--
        body, td { font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;  font-size: x-small;  color: Black;   }
        button { background-color: #EEEEEE;  border: 1px ridge Gray;  font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;  font-size: xx-small;  font-weight: bold;     }
        input { background-color: #EEEEEE;  border: 1px solid Black;  font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;  font-size: xx-small;  font-weight: bold;     }
    -->
    </style>
</head>
<body>
END;





if(!isset($isokay)) {
    echo "<table cellspacing=0 cellpadding=0 width=\"100%\" height=\"100%\">";
    echo "<tr><td align=\"center\" valign=\"center\">";

    echo "<h3>Error</h3>";
    echo "<table width=400>";
    echo "<tr><td>You must log in first.</td></tr>";
    echo "<tr><td><div align=\"right\"><input type=\"button\" value=\"Log In »\" onClick=\"window.location='index.php'\"></div></td></tr>";
    echo "</table>";

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

else    {
    $dir = opendir(".") or die("Cannot open directory.");

    echo "<form action=\"del.php\" method=\"post\">";
    echo "<center>";
    echo "<table width=\"90%\" cellspacing=\"1\" cellpadding=\"4\">";
    echo "<tr>";
    echo "<td bgcolor=\"#8F9AB7\" width=\"20\">&nbsp;</td>";
    echo "<td bgcolor=\"#8F9AB7\"><b>File name</b></td>";
    echo "<td bgcolor=\"#8F9Ab7\" width=\"100\"><b>File Size (b)</b></td>";
    echo "<td width=\"130\" bgcolor=\"#8F9AB7\"><b>Last Accessed</b></td>";
    echo "<td width=\"130\" bgcolor=\"#8F9AB7\"><b>Last Modified</b></td>";
    echo "</tr>";

    $counter = 1;
    while($file = readdir($dir))    {
        if($file != "." && $file != ".." && $file != "index.php" && $file != "main.php" && $file != "del.php" && is_file($file))   {
            if($counter == 1)   {
                echo "<tr>";
                echo "<td bgcolor=\"#DCE2F2\"><center><input type=\"checkbox\" name=\"delit[]\" value=\"$file\"><center></td>";
                echo "<td bgcolor=\"#DCE2F2\">$file</td>";
                echo "<td bgcolor=\"#DCE2F2\">".filesize($file)."</td>";
                echo "<td bgcolor=\"#DCE2F2\">".date("M j y, H:i", fileatime($file))."</td>";
                echo "<td bgcolor=\"#DCE2F2\">".date("M j y, H:i", filectime($file))."</td>";
                echo "</tr>";
                $counter++;
            }

            else    {
                echo "<tr>";
                echo "<td bgcolor=\"#CFD5E5\"><center><input type=\"checkbox\" name=\"delit[]\" value=\"$file\"><center></td>";
                echo "<td bgcolor=\"#CFD5E5\">$file</td>";
                echo "<td bgcolor=\"#CFD5E5\">".filesize($file)."</td>";
                echo "<td bgcolor=\"#CFD5E5\">".date("M j y, H:i", fileatime($file))."</td>";
                echo "<td bgcolor=\"#CFD5E5\">".date("M j y, H:i", filectime($file))."</td>";
                echo "</tr>";
                $counter = 1;
            }
        }
    }

    echo "<tr><td colspan=\"5\" bgcolor=\"#8F9AB7\"><div align=\"right\"><input type=\"submit\" value=\"Delete Selected\"></div></td></tr";
    echo "</table>";
    echo "</center>";
    echo "</form>";
}




echo <<< END
</body>
</html>
END;

?>

del.php
PHP:
<?php

session_start();

echo <<< END
<html>
<head>
    <title>Delete</title>
    <style type="text/css">
    <!--
        body, td { font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;  font-size: x-small;  color: Black;   }
        button { background-color: #EEEEEE;  border: 1px ridge Gray;  font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;  font-size: xx-small;  font-weight: bold;     }
        input { background-color: #EEEEEE;  border: 1px solid Black;  font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;  font-size: xx-small;  font-weight: bold;     }
    -->
    </style>
</head>
<body>
END;





if(!isset($isokay)) {
    echo "<table cellspacing=0 cellpadding=0 width=\"100%\" height=\"100%\">";
    echo "<tr><td align=\"center\" valign=\"center\">";

    echo "<h3>Error</h3>";
    echo "<table width=400>";
    echo "<tr><td>You must log in first.</td></tr>";
    echo "<tr><td><div align=\"right\"><input type=\"button\" value=\"Log In »\" onClick=\"window.location='index.php'\"></div></td></tr>";
    echo "</table>";

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

else    {
    $files = $_POST['delit'];
    $howmany = count($files);

    for($i=0; $i<$howmany; $i++) {
        echo $files[$i];
        if(@unlink($files[$i]))
            echo "<b>$files[$i] - Deleted</b><br>";
        else
            echo "<b>$files[$i] - Could NOT be deleted</b><br>";
    }

    echo "<br><br>";
    echo " <input type=\"button\" value=\"Continue\" onClick=\"window.location='main.php'\">";
}


echo <<< END
</body>
</html>
END;

?>

Or you can download the zip I put together for you: <<link removed, if you want it, make the files yourself>>

And a preview for you: <<link removed, if you still want to see it in action, make the files yourself =P>>

Hope this makes your deleting fun a litter.. erm.. funner :D

edit: make sur eyou chmod the directory :)
edit: feel free to try and delete the files, they will. just let me know if there's non left, some guests or someone might take the oportunity to delete the blank text files ;)
edit: yes, edit again! just noticed you wanted the file size also, just added it.
 
Last edited:
Back
Top