473,666 Members | 2,101 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

recursive delete all files/folders

Dear guru

I want to delete all file and folder recursivly under php code, can
anyone give me commend for this.

Thank very much

Sep 13 '05 #1
5 20096
be*******@gmail .com wrote:
Dear guru

I want to delete all file and folder recursivly under php code, can
anyone give me commend for this.

Thank very much


Hi,

It is not too complicated.
But pay attention to potential problems concerning file and directory
permissions.
Remember that PHP runs as user 'nobody' or 'apache' or 'www-data' on most
*nix machines, and as IUSR_XXX on W$ machines/IIS.

That user needs appropriate right to delete files and directories.

For some reference and code-examples, start reading here:
http://nl2.php.net/dir
and
http://nl2.php.net/manual/en/ref.filesystem.php

You will probably use functions like rmdir() and unlink().

Good luck!

Regards,
Erwin Moller

Sep 13 '05 #2
Something like the following should work. Untested though.

function delete_dir($pat h) {
$files = glob("$path/*");
foreach($files as $file) {
if(is_dir($file ) && !is_link($file) ) {
delete_dir($fil e);
}
else {
unlink($p);
}
}
rmdir($path);
}

Sep 13 '05 #3

deletes a file, or a folder and everything in it, whatever you put into
$dirname.

function rmdirr($dirname )
{
// Sanity check
if (!file_exists($ dirname)) {
echo "no directory by that name";
return false;
}

// Simple delete for a file
if (is_file($dirna me)) {
return unlink($dirname );
}

// Loop through the folder
$dir = dir($dirname);
while (false !== $entry = $dir->read()) {
// Skip pointers
if ($entry == '.' || $entry == '..') {
continue;
}

// Recurse
rmdirr("$dirnam e/$entry");
}// end while looping

// Clean up
$dir->close();
return rmdir($dirname) ;

}

Sep 13 '05 #4
be*******@gmail .com wrote:
Dear guru

I want to delete all file and folder recursivly under php code, can
anyone give me commend for this.

Thank very much

I wrote a function to do that a few weeks ago that was based on a
recursive copy function (for an application script on my local Windows
machine), but when it came time to test it, I chickened out and just
wrote a script to remove the specific folders used in my application.

Be careful. Those files do not go to the recycle bin.

--
*************** **************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*************** **************
Sep 14 '05 #5
be*******@gmail .com wrote:
Dear guru

I want to delete all file and folder recursivly under php code, can
anyone give me commend for this.


/// Clean directory
/** Delete all files in directory
* @param $path directory to clean
* @param $recursive delete files in subdirs
* @param $delDirs delete subdirs
* @param $delRoot delete root directory
* @access public
* @return success
*/
function cleanDir($path, $recursive=true ,$delDirs=false ,$delRoot=null)
{
$result=true;
if($delRoot===n ull) $delRoot=$delDi rs;
if(!$dir=@dir($ path)) return false;
while($file=$di r->read())
{
if($file==='.' || $file==='..') continue;

$full=$dir->path.DIRECTORY _SEPARATOR.$fil e;
if(is_dir($full ) && $recursive)
{
$result&=filesy s::cleanDir($fu ll,$recursive,$ delDirs,$delDir s);
}else if(is_file($ful l))
{
$result&=unlink ($full);
}
}
$dir->close();
if($delRoot)
{
$result&=rmdir( $dir->path);
}
return $result;
}
Oct 7 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
8331
by: Paul | last post by:
I'm trying to make get my app to delete all the files in a specified folder and all the files within the folders of the specified folder. e.g. Folder 1 contains three files (File1, File2, File3) and two folders (Subfolder 1, Subfolder 2). .......I need to delete File1, File2, File3. Subfolder 1 contains FileA. .......Need to delete FileA. Subfolder 2 contains FileB, FileC, FileD.
10
2919
by: Dan | last post by:
Hi - I'm about a week into learning VB.NET, and I'm finding I can't delete any of the VB.NET directory structures that contain my test projects I've been trying to create. I've never seen this before... Whenever I try, it says something like "access is denied" and "make sure the disk is not write-protected and file is not currently in use". Is there something VB.NET does that would cause this? I'm starting to build up a bunch of test...
7
2105
by: =?Utf-8?B?RGF2aWQgQ29sbGl2ZXI=?= | last post by:
Hi all, using C#, .NET 1.1, ASP.NET I have created a recursive??? function. It looks a little like... MyContext.Current.Folder.Parent.Parent.Parent.Parent.Name As you can see, Parent is the recursive??? part.
4
2208
by: - HAL9000 | last post by:
When un-installing an application... Is it normal practice to write a special program that erases all the files and folders for all the users of an application that reads and writes to SpecialFolder.ApplicationData (C:\Documents and Settings\Username\Application Data) ? Or, is there some way to get the ms installer to do this for you? Is there some way to use Application.UserAppDataPath to ease finding
2
13258
by: halimunan | last post by:
hello all, how do i delete all folders and files inside a directory while at the same time keeping(not deleting the directory) ? e.g : i want to delete all folders and files inside c:\temp it seems like i need to use file.delete and directory.delete at the same time.
2
2393
by: sebastien.abeille | last post by:
Hello, I would like to create a minimalist file browser using pyGTK. Having read lot of tutorials, it seems to me that that in my case, the best solution is to have a gtk.TreeStore containing all the files and folders so that it would map the file system hierarchy.
3
3384
by: Matt | last post by:
I have several folders on a server that contains files that go back 3-4 years. It was mentioned that keeping files that long is no longer needed. So my question is, is there a way to create a windows service in C# that can delete files in the folders that are older then 12 months? The server is structured something like this: DataLogs Server1 App1 - this folder contains all the log files or it can look something like this: DataLogs
9
1709
by: Lloyd Sheen | last post by:
For all those who don't think that a recursive search of files in folders is a good thing in the Microsoft.VisualBasic.FileIO.FileSystem namespace listen to this. I am reorg my mp3 collection. I have written my own catalog program and it has xml files to cache info from tags etc. To ensure that the process goes ok I went to delete all the xml files to force a rescan of the tags. This should be simple ..... but this is Vista. So...
0
2945
by: wolfsbane | last post by:
Alright, here it is I am trying to write a win32 app in VB 2005 to clean up user's profiles. everything works correctly except for the Delete("directory", True) statement. I get a System.IO.DirectoryNotFoundException: Could not find a part of the path. Then it gives me a random file name from the UserProfile\Local Settings\Temporary Internet Files\Content.IE5\SomeRandomFolder. This is absolutley driving me nuts. So I am gonn give you a basic...
0
8449
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8360
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8876
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8556
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8642
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7387
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2774
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1777
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.