Need Help in Deleting Files
Posted: Sun Feb 03, 2008 2:19 am
Hey Guys!
Im Dinesh and I'm quite new to php. I have a php program which shows folders and files in the current directory. It shows the size of the file or folder, type, Last accessed, Last modified. Everything works cool, But I need one thing to work. when i click the check box it should delete the file or folder. For that what i did is I passed a javascript code saying that when the checkbox is clicked it will prompt for confirmation. if the user confirms ok it will call a javascript function to call another php function which will delete the particular file or folder. when i click the checkbox the javascript code sends the value to another javascript function from which the value will be given to an php function. The php function gets the value and check if there is any file name related to it. The program works cool, it returns value to php and with the echo statement the value is displaying cool, but when i check for file in the destination directory with the submitted value it shows that the file is not found. So that the php cannot delete the file. Please help me in it. I think this is a small bug but i can't rectify it.
For a clear code please check:
http://thecurse.newsit.es/Bug/
Awaiting for the clarification
Dinesh
For a clear code please check:
http://thecurse.newsit.es/Bug/
Code: Select all
<html><head><title>Directory Tree</title></head>
<body><center>
<?php
#Open Directory
$dirname = ".";
$count = 0;
$dir = opendir($dirname);
$files_total = array();
$back_arrow = "images/ascend.png";
$check_dir = "../log/";
if (!is_file($back_arrow))
{
$back_arrow = "../$back_arrow";
if(!is_file($back_arrow))
{
$back_arrow = "../$back_arrow";
}
else
{
$back_arrow = "$back_arrow";
}
}
else
{
$back_arrow = "$back_arrow";
}
if (!is_dir($check_dir))
{
$back_dir = "javascript:location.href('../');";
}
else
{
$back_dir = "javascript:alert('No Directories Available')";
}
$file_list.="<tr><td colspan=5 style=\"background-color:#99ff99;cursor:hand;\" onMouseOver=\"this.style.backgroundColor='yellow';this.style.fontWeight='bold';\" onMouseOut=\"this.style.backgroundColor='99ff99';this.style.fontWeight='normal';\" onClick=\"$back_dir\"><center><img src=\"$back_arrow\"><br>Back</img></center></td></tr>";
$file_list.="<tr style=\"cursor:hand;\"><th>Name</th><th>Type</th><th>Size</th><th>Last Accessed</th><th>Last Changed</th></tr>";
$file_list.="<tr><td></td></tr>";
while( false != ($file_dir = readdir($dir)))
{
if (($file_dir != "." ) and ($file_dir != "..") and ($file_dir != "index.html") and ($file_dir != "index.php") and ($file_dir != "log.txt") and ($file_dir != "bkp"))
{
$file_size = filesize($file_dir);
$file_access = date("F d Y H:i:s.", fileatime($file_dir));
$file_change = date("F d Y H:i:s.", filectime($file_dir));
$type = filetype("$dirname/$file_dir");
array_push($files_total, $file_dir);
if ($type == dir)
{
$file_type = "[ File Folder ]";
$method = "_self";
$file_nme = ucfirst("$dirname/$file_dir");
$nm = split("[/\\.]", $file_nme);
$e = count($nm)-1;
$nm = $nm[$e];
$file_name = $nm;
$file_name = ucfirst($file_name);
$method = "_self";
$file_size = get_size($file_dir);
if ($file_size == null)
{
$file_size = "[ Empty Folder ]";
}
else
{
$file_size = $file_size." Bytes";
}
}
else
{
$file_nme = ucfirst("$dirname/$file_dir");
$nm = split("[/\\.]", $file_nme);
$e = count($nm)-2;
$nm = $nm[$e];
$file_name = $nm;
$file_name = ucfirst($file_name);
$method = "_blank";
$file_ext = ucfirst("$dirname/$file_dir");
$exts = split("[/\\.]", $file_ext) ;
$n = count($exts)-1;
$exts = $exts[$n];
$exts = ucfirst($exts);
$file_type = "$exts File";
$file_size = "$file_size Bytes";
}
# echo $file_dir;
$file_list .= "<tr onMouseOver=\"this.style.backgroundColor='yellow';\" onMouseOut=\"this.style.backgroundColor='white';\"><td>";
$file_list .= "<input type=\"checkbox\" onClick=\"if(this.checked){z=confirm('Are you Sure You Want to Delete the File');if(z==true){delFile('$count')}else{window.history.go(0);};}\">";
$file_list .= " <a href=\"$file_dir\" target=\"$method\" style=\"text-decoration:none;\">$file_name</a></td><td class=cen>$file_type</td><td class=cen>$file_size</td><td class=cen>$file_access</td><td class=cen>$file_change</td></tr>";
$count++;
// echo $files_total[2];
}
# $tot_file_size = count($files_total);
}
function get_size($path)
{
if(!is_dir($path))return filesize($path);
$dir = opendir($path);
while($file = readdir($dir))
{
if(is_file($path."/".$file))$file_size+=filesize($path."/".$file);
if(is_dir($path."/".$file) && $file!="." && $file !="..")$file_size +=get_size($path."/".$file);
}
return $file_size;
}
function deleteFile($file_delete)
{
echo $file_delete."<br>";
$file_no = $file_delete;
$dir_name = ".";
$drnm = opendir($dir_name);
$file_arr = array();
while (false != ($file_nm = readdir($drnm)))
{
if (($file_nm != ".") && ($file_nm != "..") && ($file_nm != "index.php"))
{
echo $file_nm."<br>";
array_push ($file_arr, $file_nm);
}
}
$arr_size = count($file_arr);
echo $arr_size."<br><br><br>";
echo $file_no."<br>";
if ($file_no == 0)
{
echo $file_arr[0];
}
if ($file_no == 1)
{
echo $file_arr[1];
}
if ($file_no == 2)
{
echo $file_arr[2];
}
if ($file_no == 3)
{
echo $file_arr[3];
}
if ($file_no == 4)
{
echo $file_arr[4];
}
if ($file_no == 5)
{
echo $file_arr[5];
}
closedir($drnm);
}
function downloadFile()
{
}
closedir($dir);
echo $tot_file_size;
echo "<table border='0' cellspacing='0' cellpadding='0'>";
echo $file_list;
echo "<tr><td></td></tr>";
echo "<tr><td style=\"border: 1px dotted black\" colspan=\"5\" align=\"right\"><input class=\"button\" type=\"button\" name=\"delete\" value=\"Delete File\"> <input class=\"button\" type=\"button\" name=\"download\" value=\"Download File\"> </td></tr>";
echo "</table>";
?>
</center>
</body>
</html>
<script language="javascript">
function delFile(file_del)
{
document.write("<?php deleteFile('");
document.write(file_del);
document.write("') ?>");
}
</script>
<style>
body{
font-family:arial;
font-size: 13;
}
a{
color:black;
text-decoration:none;
}
td{
width:150;
height:50;
font-size: 13;
}
th{
width:150;
height:50;
border:1px black;
font-size: 12;
border: 1px dashed black;
}
.cen{
text-align:center;
}
</style>