Jump to content

opendir . and ..


freelance84

Recommended Posts

I am learning about reading file directories and found this from thi example of opendir from phpmanual 

// Open a known directory, and proceed to read its contents
$dir = "../public_html/";
$directoryArray = array();
$fileArray = array();

if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
        
        if(filetype($dir . $file) == 'file')
        	{
	        	array_push($fileArray,$file);
        	}
        else{
	        	array_push($directoryArray,$file);
        	}
        }
        closedir($dh);
    }
}
asort($directoryArray);
asort($fileArray);

print_r($directoryArray);
print_r($fileArray);

 

The first 2 directories printed out are...

Array
(
    [0] => .
    [1] => ..

 

Now these are not folders I created... are they built in shortcuts for when you type ../somfile.php or  ./somfile.php ??

Link to comment
Share on other sites

Yes, in *nix and windows . represents the current directory and .. represents the parent directory.  In *nix they are actual directories.

 

BTW, I prefer glob() for what you are doing:

 

$directoryArray = glob($dir, GLOB_ONLYDIR);
$fileArray = array_diff(glob($dir), $directoryArray);

Link to comment
Share on other sites

BTW, I prefer glob() for what you are doing:

 

$directoryArray = glob($dir, GLOB_ONLYDIR);
$fileArray = array_diff(glob($dir), $directoryArray);

 

Interesting approach to get only the files using glob() with GLOB_ONLYDIR and array_diff(). I don't know why there isn't a flag such as GLOB_ONLYFILE Anyway, here is another approach that may or may not be more efficient. It only requires one glob() call, but then filters based on is_file();

$files = array_filter(glob($path.'*'), 'is_file');

Link to comment
Share on other sites

Good, point.  I remember using that and 'is_dir' in a post in the past.  One thing I would point out though, is running that on a dir with many files may be slower as the is_file() will need to stat all of the files/dirs in the directory, unless the glob has already populated the stat cache (I don't know).

Link to comment
Share on other sites

Cool thanks... that's some pretty extreme optimizing of my attempt...

 

Although i can't seem to get it to return the .htaccess file with:

$files = array_filter(glob($path.'*'), 'is_file');

 

With my OP, the two arrays returned absolutely everything within in the $dir. What i am doing is creating a file management page, one which 'locks' files when they are being worked on to prevent anyone else from editing the same file and thus wasting time. As a result I need to be able to see all files. I'm assuming that the .htaccess is being filtered by the is_file()?

 

PS/ thanks as well, I hadn't come across array_filter before, or glob. Cheers  :D

 

PPS/ With the array_filter and the asort, both methods seem to sort alphabetically the upper case first and then the lower case... Is there anyway of getting the results to be case insesitive as to their order?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.