Page 1 of 1

omiting the Mac "._" metafiles while looping...

Posted: Tue Jul 17, 2007 3:47 pm
by nyghtrunner
Hey all,

I have what should be a pretty easy question. I'm building a PHP page to convert image types on the backend, but my testing has run me into a few problems... The biggest one is this: My entire office uses Macintosh computers (with like 3 exceptions...), and so almost all of our assets are stored on our servers with the "._" prefix that names them as a Macintosh metafile.

Now, sometimes this isn't really an issue, but it seems to be when I try to loop through a directory to grab the files, as it also grabs and returns those stoopid metafiles as well, which I don't want at all! It's not just this, but I have been able to bootleg my way around it with comparison values, but I am really getting tired of having to invent new ways to get around these files, and deleting them isn't much of an option, as any time they are accessed by anyone on the network, they'll most likely just get new ones assigned to them...

So my question is this: With this snippet of code, anyone know how I can exclude those files from the loop?

Code: Select all

<?php

include("class.imageconverter.php");
$dir = "GIFs/";
$handle=opendir($dir); //current directory, can be changed

$counter = 0;

while ($file = readdir($handle)) {
//check file type for images
	$the_type = strrchr($file, ".");
	$is_picture = eregi("jpg|gif|bmp|png",$the_type); //adjust for other image types

	if ($file != "." and $file != ".." and $is_picture) {
		$mypics[$counter] = "GIFs/".$file;
		//$size[$counter] = getimagesize($mypics[$counter]);
		//print $mypics[$counter];
		$img = new ImageConverter($mypics[$counter],'jpg');
		$counter++;
	} 
	echo $mypics[$counter];
}
print $counter;
closedir($handle);

?>
Thanks for any help in advance!

Stephen

Posted: Tue Jul 17, 2007 3:52 pm
by Begby
Where you are checking if the file is '.' or '..' just put in anther check to see if it starts with '._'

Posted: Tue Jul 17, 2007 4:10 pm
by nyghtrunner
I tried that, but it doesn't seem to be working... Here's some of the "error" message I get.

"Warning: imagecreatefromgif(._147.gif) [function.imagecreatefromgif]: failed to open stream: No such file or directory in /web/www.edgarandellen.com.dev/html/images/f ... verter.php on line 78"

And that is with this code:

Code: Select all

<?php

include("class.imageconverter.php");
$dir = "GIFs/";
$handle=opendir($dir); //current directory, can be changed

$counter = 0;

while ($file = readdir($handle)) {
//check file type for images
	$the_type = strrchr($file, ".");
	$is_picture = eregi("jpg|gif|bmp|png",$the_type); //adjust for other image types

	if ($file != "." and $file != ".." and $file != "._" and $is_picture) {
		$mypics[$counter] = $file;
		//$size[$counter] = getimagesize($mypics[$counter]);
		$img = new ImageConverter($mypics[$counter],'jpg');
		$counter++;
	} 
	echo $mypics[$counter];
}
print $counter;
closedir($handle);

?>
But looking at the error, it's obviously reading the "._147.gif" metafile, and treating it as though it were an image, which it isn't really, and so it seems to be causing all kind of problems.

Is this what you meant? Or did you mean somewhere/something else?

Stephen

Posted: Tue Jul 17, 2007 4:16 pm
by John Cartwright
try

Code: Select all

if ($file != "." and $file != ".." and substr($file, 0, 2) != "._" and $is_picture) {
substr() is your friend

Posted: Tue Jul 17, 2007 4:25 pm
by nyghtrunner
Beautiful! That worked like a charm! Thanks so much for the tip!! :D

Now if only I can figure out why I'm still getting the error... But that's another issue entirely that will require debugging... :evil: