Mourning the loss of our friend, WhitPhil.
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
 
Web Design & Development
Tag Cloud
access audio black screen blue screen boot bsod connection crash dell desktop driver dvd email error excel excel 2003 firefox hard drive hardware hijackthis internet keyboard laptop malware monitor network networking outlook problem processor ram recovery router safe mode screen slow sound spyware tdlwsp.dll trojan vba video virus vista vundo windows windows 7 windows vista windows xp wireless
Search
Search for:
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
Solved: how to delete and directory and its contents with PHP

Tip: Click here to scan for System Errors and Optimize PC performance
[ Sponsored Link ]

 
Thread Tools
emopoops's Avatar
emopoops has a Photo Album
Computer Specs
Senior Member with 183 posts.
 
Join Date: Jan 2009
Location: america c- ountry
Experience: pretty experinced ;]
23-Oct-2009, 04:22 PM #1
Question Solved: how to delete and directory and its contents with PHP
HOW DO U DO IT? i need to know the php coding
cwwozniak's Avatar
Distinguished Member with 17,047 posts.
 
Join Date: Nov 2005
Location: McHenry, IL - USA
Experience: Enough to be dangerous
23-Oct-2009, 04:55 PM #2
I believe you first need to individually delete each file in the directory and then delete the empty directory.

The unlink( ) function deletes a specific file.
http://www.w3schools.com/php/func_filesystem_unlink.asp

The rmdir( ) function deletes a specific empty directory
http://www.w3schools.com/PHP/func_filesystem_rmdir.asp

You can use the scandir( ) function to build an array containing the file names in a directory.
http://www.w3schools.com/php/func_directory_scandir.asp
__________________
Chuck W.
emopoops's Avatar
emopoops has a Photo Album
Computer Specs
Senior Member with 183 posts.
 
Join Date: Jan 2009
Location: america c- ountry
Experience: pretty experinced ;]
24-Oct-2009, 08:23 AM #3
why isnt this working for me? i want to delete the directory and the contents:
Code:
<?php 
$documentroot = $_SERVER['DOCUMENT_ROOT'];

$username = "Administrator";
$dir = $documentroot . '/' . $username;
function rmdir_r ( $dir, $DeleteMe = TRUE )
{
    if ( ! $dh = @opendir ( $dir ) ) return;
    while ( false !== ( $obj = readdir ( $dh ) ) )
    {
        if ( $obj == '.' || $obj == '..') continue;
        if ( ! @unlink ( $dir . '/' . $obj ) ) rmdir_r ( $dir . '/' . $obj, true );
    }
    
    closedir ( $dh );
    if ( $DeleteMe )
    {
        @rmdir ( $dir );
    }
}
?>
cwwozniak's Avatar
Distinguished Member with 17,047 posts.
 
Join Date: Nov 2005
Location: McHenry, IL - USA
Experience: Enough to be dangerous
24-Oct-2009, 11:21 AM #4
I am still a rank beginner as far as writing and debugging PHP is concerned. It may take me some time to go through your function. Anyone else here is more than welcome to jump in and help out.
emopoops's Avatar
emopoops has a Photo Album
Computer Specs
Senior Member with 183 posts.
 
Join Date: Jan 2009
Location: america c- ountry
Experience: pretty experinced ;]
24-Oct-2009, 11:32 AM #5
lol yeah simple thing i couldnt figure out. someone had to tell me i have to call the function! lol cause it wasnt being called so i had to add
rmdir_r($dir); at the bottom(not in the function) i thot it was huge time concerning thing too! until someone pointed out the "obvious" i guess i just get put off by big lines of code LOL. thanks for replying
cwwozniak's Avatar
Distinguished Member with 17,047 posts.
 
Join Date: Nov 2005
Location: McHenry, IL - USA
Experience: Enough to be dangerous
24-Oct-2009, 05:46 PM #6
Glad to hear you figured it out. I probably would not have thought of that little detail myself.
emopoops's Avatar
emopoops has a Photo Album
Computer Specs
Senior Member with 183 posts.
 
Join Date: Jan 2009
Location: america c- ountry
Experience: pretty experinced ;]
25-Oct-2009, 05:41 PM #7
thanks
Reply Bookmark and Share

Smart Search

Find your solution!



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.

Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -5. The time now is 12:39 PM.
Copyright © 1996 - 2009 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.
Powered by Cermak Technologies, Inc.