tags:

views:

85

answers:

4
   $dir_handle = @opendir($url) or die("Unable to open $url");
   $count = "0";
   while ($file = readdir($dir_handle)) {
      if (!is_dir($url.'/'.$file) && ($file="*.jpg" || $file="*.gif" || $file="*.png") && $file!="picture0.*") {
         $galleryEventFile[$count] = $file;
         $count++;
      }
   }
   closedir($dir_handle);

I think it has something to do with this line:

if (!is_dir($url.'/'.$file) && ($file="*.jpg" || $file="*.gif" || $file="*.png") && $file!="picture0.*")

but im not sure

A: 

First, $count should be a number. Do:

 $count = 0;

Second, AFAIK, PHP doesn't support wildcard matching like that. You can't use "*" to match. You'll need to use regular expressions to match in the conditional.

thedz
That shouldn't be a problem. I just tried `$count = "0"; $count++; echo $count;` and it echo'ed 1.
jimyi
Why 1 as opposed to 0? Doesn't make any sense.
hobodave
That's true. It was more stylistic than anything. The implicit type conversion bothers me. The real problem I think is the wildcard matching with "*"
thedz
@jimyi: regardless, it's a bad idea to count by incrementing a string.
hobodave
@hobodave: oops, that was a typo. should be 0. thanks for the catch!
thedz
+6  A: 

I can see two things that will be causing you problems:

Assignment/comparison:

You have the code:

if ($file="*.jpg" //etc...

However, a single equal sign will perform an assignment, not a comparison - you need to use two equals signs (==) for this. See http://php.net/manual/en/language.operators.comparison.php. Essentially what you are doing by doing an assignment in an if statement is:

$file = '*.jpg';
if ($file) { }

Wildcard matching of strings

You also can't do wildcard matching like that ($file == "*.jpg) on a string, you could look at using preg_match() and regular expressions instead, e.g.

if (!preg_match('/\.jpg$/i', $file)) {
    //not .jpg
}

It might be better to do something like this though:

//get file extension
$extension = pathinfo($file, PATHINFO_EXTENSION);

$allowedExtensions = array('jpg', 'png', 'gif');

//check in allowed list
if (!in_array(strtolower($extension), $allowedExtensions)) {
    //not valid
}
Tom Haigh
why is it $pathinfo rather than $extension?
imHavoc
because I'm an idiot. thanks, have corrected
Tom Haigh
A: 

Do as thedz and Tom Haigh have suggested.

Have you also heard about XDebug? This will allow you to setup an environment say using Eclipse and step through your PHP code. I do not develop without using a combination Eclipse and XDebug.

Wayne
A: 

The first thing you want to do is to debug the if line. Remember that if you put *.gif, it is looking to see that the file is actually named "*.gif", rather than looking for 'any' gif file, similar to what Windows does.

What I'd suggest is going through each segment of the if, and get it to pass. then you can start putting it together.

Chacha102