为什么$ count不更新?

imH*_*voc 0 php readdir

   $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);
Run Code Online (Sandbox Code Playgroud)

我认为这与这一行有关:

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

但我不确定

Tom*_*igh 6

我可以看到两件会导致你出现问题的事情:

分配/比较:

你有代码:

if ($file="*.jpg" //etc...
Run Code Online (Sandbox Code Playgroud)

但是,单个等号将执行赋值,而不是比较 - 您需要使用两个等号(==).请参见http://php.net/manual/en/language.operators.comparison.php.基本上你在if语句中进行赋值的工作是:

$file = '*.jpg';
if ($file) { } 
Run Code Online (Sandbox Code Playgroud)

字符串的通配符匹配

你也不能在字符串上做那样的通配符匹配($ file =="*.jpg),你可以看一下使用preg_match()和正则表达式,例如

if (!preg_match('/\.jpg$/i', $file)) {
    //not .jpg
}
Run Code Online (Sandbox Code Playgroud)

这样做可能会更好:

//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
}
Run Code Online (Sandbox Code Playgroud)