Page 1 of 1

Skip over ._ mac files

Posted: Wed Oct 05, 2011 10:48 pm
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!

Re: Skip over ._ mac files

Posted: Wed Oct 05, 2011 11:16 pm
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

Re: Skip over ._ mac files

Posted: Thu Oct 06, 2011 9:12 pm
by rudy_vail
Awesome, that worked great, thanks!