为什么 $count 没有更新?
$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);
我认为这与这一行有关:
if (!is_dir($url.'/'.$file) && ($file="*.jpg" || $file="*.gif" || $file="*.png") && $file!="picture0.*")
但我不确定
$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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我可以看到两件事会给您带来问题:
赋值/比较:
您有代码:
但是,单个等号将执行赋值,而不是比较 - 您需要使用两个等号(==) 为此。 请参阅 http://php.net/manual/en/language.operators.comparison .php。 本质上,您通过在 if 语句中进行赋值所做的事情是:
字符串的通配符匹配
您也不能在字符串上进行像 ($file == "*.jpg) 这样的通配符匹配,您可以考虑使用 preg_match() 和正则表达式,例如,
这样做可能会更好:
I can see two things that will be causing you problems:
Assignment/comparison:
You have the code:
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:
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.
It might be better to do something like this though:
首先,$count 应该是一个数字。 做:
其次,据我所知,PHP 不支持这样的通配符匹配。 不能使用
"*"
来匹配。 您需要使用正则表达式来匹配条件。First, $count should be a number. Do:
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 和 Tom Haigh 的建议进行操作。
您还听说过 XDebug 吗? 这将允许您使用 Eclipse 设置环境并逐步执行 PHP 代码。 我不会在不使用 Eclipse 和 XDebug 组合的情况下进行开发。
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.
您要做的第一件事是调试 if 行。 请记住,如果您输入
*.gif
,它会查找该文件实际上名为"*.gif"
,而不是查找“任何”gif 文件,与 Windows 的做法类似。我的建议是仔细检查 if 的每个部分,并使其通过。 然后你就可以开始把它放在一起了。
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.