Skip over ._ mac files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rudy_vail
Forum Newbie
Posts: 2
Joined: Wed Oct 05, 2011 10:27 pm

Skip over ._ mac files

Post by rudy_vail »

I need to make an array of file names from a directory.
I'm using this:

Code: Select all

if ($handle = opendir($image_dir)) {
			while (false !== ($file = readdir($handle))) 
			{
				if ($file != '.' && $file != '..' && $file != '._*') 
				{
						$files[] = $file;
				}
			}
			closedir($handle);
		}[ /syntax]

It works, but the people who are putting the files in the directories are using macs, so there are the ._ resource fork files and these get included in the array, which messes other things up.

I was hoping that "!= '._*'" would take care of it, but it doesn't. Is there any wildcard character I can use or will i have to work something out with a substring?
I'm not very good with php, so any help you guys could offer would be greatly appreciated. Thanks!
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Skip over ._ mac files

Post by twinedev »

For this, I would just do the substr():

Code: Select all

if ($handle = opendir($image_dir)) {
    while (false !== ($file = readdir($handle)))
    {
        if (substr($file,0,2) != '._' && $file != '.' && $file != '..')
        {
            $files[] = $file;
        }
    }
    closedir($handle);
}
Note, I moved this check to be the first one, as you want to put them in order that you expect to them to break the logic (ie, you will only have one . and one .. entry, where you will probably have more than one ._ files, so check that match first)

-Greg
Last edited by twinedev on Thu Oct 06, 2011 9:13 pm, edited 1 time in total.
rudy_vail
Forum Newbie
Posts: 2
Joined: Wed Oct 05, 2011 10:27 pm

Re: Skip over ._ mac files

Post by rudy_vail »

Awesome, that worked great, thanks!
Post Reply