View Full Version : Help with deleting files

11-20-2006, 08:53 PM

Im running into a problem, I have a single page login that displays the contents of a directory....


<script LANGUAGE="JavaScript">

function confirmSubmit()
var agree=confirm("This will delete this File Permanently. Do you want to continue?");
if (agree)
return true ;
return false ;
// -->

$username = "xxxxxxx";
$password = "xxxxxxx";

if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) {



<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<h1>Login <br> Holidays Get Published</h1><br>
<p><label for="txtUsername">Username:</label> <br />
<input type="text" title="Enter your Username" name="txtUsername" /></p>
<p><label for="txtpassword">Password:</label> <br /><input type="password" title="Enter your password" name="txtPassword" /></p>
<p><input type="submit" name="Submit" value="Login" class="buttons"/><br><br></p></form>


else {



$dir = 'xxxxx/xxxxx/xxxxxx/';
$filecount = 0;
$d = dir($dir);
while ($f = $d->read()) {
if(($f!= ".") && ($f!= "..")) {
if(!is_dir($f)) $filecount++;
echo 'there are currently ',$filecount,' files <br><br>';

$rep = "xxxxxxxx/xxxxxx/xxxx/";
$dir = opendir($rep);

function dd($date) {
return date("m/d/Y H:i:s",$date);

while ($f = readdir($dir)) {
if(is_file($rep.$f)) {
echo "<div class='box'>";
echo "<li>Name: $f";
echo "<li><b><a href='xxxxxxxx/$f'>Download</a></b>&nbsp;&nbsp;&nbsp;<font color=red>(Right-Click, Save As...) </font>";
echo "<li><b><a href='deletefiles.php?file=holidayuploads/$f&page=getcontent_holidays.php' onClick='return confirmSubmit()'>Delete</a></b>";
echo "<li>Size: ".filesize($rep.$f)." bytes";
echo "<li>Creation: ".dd(filectime($rep.$f));
echo "<li>Modification: ".dd(filemtime($rep.$f));
echo "<li>Thumb:<br><img src='holidayuploads/$f' width='150px'>";
echo "</div>";
echo "<br><br>";
}closedir($dir); ?>




Now the problem is when i click delete is goes to a page called deletefiles.php where i have


$do = unlink("$file");
echo "The file was deleted successfully. <meta http-equiv='refresh' content='2;url=$page'>";
} else { echo "There was an error trying to delete the file."; }


then redirects back to the main page, this is where i have a problem, everytime i do it asked me to log in agian, is there a way around this?



11-21-2006, 07:12 AM
Yes, you could use cookie or session to track whether user is valid. For example in the main php file that with login page you could add following code

if ($_POST['txtUsername'] == $username && $_POST['txtPassword'] == $password)
document.location.href = "path to main php file";
if (!isset($_COOKIE['valid_user'])) {
//show login screen
else {
//display category content