homepage Welcome to WebmasterWorld Guest from 54.205.207.53
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Php script - List All Contents but unable to deleted
chrissim

5+ Year Member



 
Msg#: 4415665 posted 12:58 am on Feb 9, 2012 (gmt 0)

hi,

The script here managed to show all files including the directories but somehow it unable to delete them. Could someone or any gurus take a look here.



<?

function list_dir($what){
if ($handle = opendir($what)) {
while (false !== ($file = readdir($handle))){
if ($file !== "." && $file !== ".." ){
if(!$files) $files=$file;
else $files = $file."\r\n".$files;
}
}
closedir($handle);
}
$arr=explode("\r\n", $files);
return $arr;
}

function delete_dir($dir){
if(is_file("$dir/$folder_or_file")){
@unlink("$dir/$folder_or_file");
} else {
$handle = opendir($dir);
while (false!==($folder_or_file = readdir($handle))) {
if($folder_or_file != "." && $folder_or_file != "..") {
if(is_dir("$dir/$folder_or_file")){
delete_dir("$dir/$folder_or_file");
} else {
@unlink("$dir/$folder_or_file");
}
}
}
closedir($handle);
if(@rmdir($dir)){
$success = true;
} else echo $error;
}
return $success;
}

if ($fid > 0 ) {
$dir_list = list_dir("./");
if (delete_dir("./" . $dir_list[($fid-1)])){
}else {
$errors =1;
$error .="Unable to remove " . $dir_list[($fid-1)]. " Most likly due to permissions, try ftp.<BR>";
}
if ($errors != 0){
echo $error;
exit;
} else


print "<html><head><META http-equiv=\"refresh\" content=\"0;URL=$_SERVER[PHP_SELF]\"></head><body></body></html>";
$refresh_mes = "Files Deleted <B><A href=\"$_SERVER[PHP_SELF]\">Refresh</A></B>";
}
$dir_list = list_dir("./");
if(sizeof($dir_list)>0){
if($dir_list[0]!= ""){
echo "$refresh_mes <BR><B>WARNING NO DELETE CONFIRMATION.</B><ol>";
for($i=0;$i<sizeof($dir_list);$i++)
echo " <li>".$dir_list[$i]." <a href='?fid=".($i+1)."'>delete?</a> <a href='".$dir_list[$i]."'>View?</a> </li>";
echo " </ol>";
}
}
?>


 

chrissim

5+ Year Member



 
Msg#: 4415665 posted 2:20 am on Feb 10, 2012 (gmt 0)

Any help to these issue?

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4415665 posted 5:03 pm on Feb 10, 2012 (gmt 0)

Your script likely doesn't have permissions:

@unlink("$dir/$folder_or_file");

See the @? It suppresses errors. Generally unlink will return a system response code, I can't recall what "no errors" will be at the moment, you'll have to check. So if you do this,

if (! $response = unlink("$dir/$folder_or_file")) {
print "<p>OOPS! Could not delete file. Response code was $response</p>";
exit;
}

Or you might need to do this

$response = unlink("$dir/$folder_or_file");

if (! $response == $whatever_the_ok_code_is) {
print "<p>OOPS! Could not delete file. Response code was $response</p>";
exit;
}

You'll do two things, find out why it couldn't delete and make good use of error trapping in your programming. :-)

Same is true here. if(@rmdir($dir)){

Also never use php_self, google php_self vulnerabilities to find out why.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved