PHP read directory

PHP read directory

Tuesday, October 30th, 2007 in PHP

The other day I found myself having a problem with not empty directories. I had to delete a directory that had another directory in it that contained a file. Giving the fact that I’m a lazy programmer, I had to find an easy enough solution to use then and every time I needed it without doing much work every time.

So I came up with a class that reads the whole directory, it stores in 2 different arrays all the files and subdirectories (the full path to them, starting with the given directory) and than it deletes them if one wants to do that. If you need such code use the one below. It worked fine for me so it should read the directories for you too. Here goes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
class read_full_dir
{
  var $dir_tree;
  /*================================================
     class constructor
     ================================================*/
  function read_full_dir( $path_to_dir )
  {
    $this->read_directory( $path_to_dir );
  }
  /*================================================
      reads the full directory tree and store it in
      $dir_tree
     ================================================*/
  function read_directory( $directory )
  {
    if ( $handle =     @opendir( $directory ) )
    {
	while ( false !== ( $file = readdir( $handle ) ) )
	{
  	  if ( $file != ".." && $file != "." )
	  {
	    if( is_dir( $directory.'/'.$file))
	    {
	      $this->dir_tree['directories'][] = $directory.'/'.$file;
	      $this->read_directory( $directory.'/'.$file );
	    }
	    else
	      $this->dir_tree['files'][] = $directory.'/'.$file;
	  }
	}
    }
  }
 
  function delete_directory()
  {
	if( is_array( $this->dir_tree['files'] ) )
	{
		foreach( $this->dir_tree['files'] as $value)
			@unlink($value);
	}
	if(is_array($this->dir_tree['directories']))
	{
		$this->dir_tree['directories'] = array_reverse($this->dir_tree['directories']);
		foreach($this->dir_tree['directories'] as $value)
			rmdir($value);
	}
  }
}
?>

To use it, you have to write only three lines of code:

1
2
3
4
5
<?php
	require_once("dir.class.php");
	$obj = new read_full_dir('path_to_dir');
	$obj->delete_directory(); // this will delete the whole directory with all subsequent files and folders
?>

Please keep in mind that if you don’t have full access to all directories, you won’t be able to delete anything.

Was this useful? Show your support.

digg PHP read directory

11 comments

  1. Hector vonRichter says:

    Hello:

    Can you provide an example on supplying the class with the directory path? I’m new to OOP and I’d like to start using classes such as this one you created. Any help would be greatly appreciated! Thank you!

  2. Hector vonRichter says:

    Well, I figured out how it works. If I run test.php, which contains the following:
    delete_directory();
    ?>

    it wipes out every file and directory!!!!!!!

    No worries, I’ll just reupload all my files.

    How would I go about adding a confirmation alert box or an “Are you sure you want to delete…” warning box to this script?

    Perhaps a warning to other viewers when experiementing with classes such as this one.

    Thank you!

  3. Hector vonRichter says:

    *This didn’t print the first time:


    delete_directory();
    ?>

  4. Hi Hector,
    I’m sorry I couldn’t answer you sooner. The thing is that what you experienced is the exact thing that should happen. The class’s purpose is to delete a directory that has subdirectories in it. The warning box that you are talking about… well, that is something you should program yourself. If you need help with it, let me know and I’ll write a tutorial for it.
    If you only want to read a directory ( sorry for the one you deleted ), use this

    < ?php
    require_once("dir.class.php");
    $obj = new read_full_dir('path_to_dir');
    print_r($obj->dir_tree);// this is just for displaying purpose. The dir tree is stored in $obj->dir_tree var
    ?>

    If you need more info, please let me know . I’ll answer all your questions in a more prompt manner. Again, sorry for the delay, I hope I did not caused you any trouble.

  5. watts says:

    Hello,
    have you an exemple to download ?

    regards

  6. watts says:

    Waou Constantin Boiangiu: you are very rapid, !!
    Mill regards, because 2 hours i’m working on this script and i have problems: i ‘m trying that :
    i m try to scan folders wtih subfolders and and create an xml.php (or xml.xml)…
    regards !!

  7. watts says:

    An other question please Constantin,
    is it possible, with an script like that to insert into mysql database ?
    because i’m using an automated webcam uploading files with folders_dated_named i want this script auto-update mysql ?
    1000 regards

    • Yes, I suppose you can do that. Depends on how exactly you plan on doing that and what you need. The directory tree is stored in $class_instance->dir_tree. This array has two keys: files and directories. To see all the files from the directory, do a loop on $class_instance->dir_tree['files']. Similar for directories. I hope this answers your question.
      As a note, when I created this, I had in mind mainly the delete function. I needed an easy way to delete not empty directories.

  8. watts says:

    ok,
    thanks !!
    i ‘m newbie in php…also,
    i ‘m searching just one php script creating .xml scanning folders & subfolders.
    i have tried your xml_try.zip and its very good for one folder but not for subfolders
    know, when i try your script ( top of this page), i can not create and .xml

    Could youy help me ?

    2000 regards !

  9. john wu says:

    Thanks so much for useful class.

Leave a comment