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

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
User avatar
nyghtrunner
Forum Newbie
Posts: 6
Joined: Fri Jul 13, 2007 3:11 pm

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

Post 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
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

Where you are checking if the file is '.' or '..' just put in anther check to see if it starts with '._'
User avatar
nyghtrunner
Forum Newbie
Posts: 6
Joined: Fri Jul 13, 2007 3:11 pm

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

try

Code: Select all

if ($file != "." and $file != ".." and substr($file, 0, 2) != "._" and $is_picture) {
substr() is your friend
User avatar
nyghtrunner
Forum Newbie
Posts: 6
Joined: Fri Jul 13, 2007 3:11 pm

Post 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:
Post Reply