为 foreach() 提供的参数无效 - 使用 glob
今天我的脚本有问题,我的脚本应该搜索(.css 文件)
我使用了代码:
- 搜索根文件夹中的所有子文件夹
- 将子文件夹的所有路径放入数组中
- 使用 foreach(){glob ....},脚本应该找到 css 文件的所有路径
这是我的代码:
$path = '';
$stack[] = $dir;
while ($stack) {
$thisdir = array_pop($stack);
if ($dircont = scandir($thisdir)) {
$i=0;
while (isset($dircont[$i])) {
if ($dircont[$i] !== '.' && $dircont[$i] !== '..') {
$current_file = "{$thisdir}/{$dircont[$i]}";
if (is_dir($current_file)) {
$path[] = "{$thisdir}/{$dircont[$i]}";
$stack[] = $current_file;
}
}
$i++;
}
}
}
$path[] = $dir;
foreach($path as $dirname){
$add = glob($dirname . '/*.css');
foreach($add as $file){
$code = file_get_contents($file);
$code_arab = arabicer($code);
file_put_contents($file,$code_arab);
}
}
当我启动脚本时,我发现一条逐渐消失的消息:
警告:在第 131 行 /home/u274517531/public_html/libs/functions.php 中为 foreach() 提供的参数无效
我确定我的数组不为空。
那么,任何人都可以帮助我如何解决这个问题?
谢谢。
Today i have a problem with my script, my script should search for (.css files)
I've used a code to:
- Search for all subfolders in a root folder
- Make all paths of subfolders in an array
- by using foreach(){glob....}, the script should found all paths of css files
Here is my code :
$path = '';
$stack[] = $dir;
while ($stack) {
$thisdir = array_pop($stack);
if ($dircont = scandir($thisdir)) {
$i=0;
while (isset($dircont[$i])) {
if ($dircont[$i] !== '.' && $dircont[$i] !== '..') {
$current_file = "{$thisdir}/{$dircont[$i]}";
if (is_dir($current_file)) {
$path[] = "{$thisdir}/{$dircont[$i]}";
$stack[] = $current_file;
}
}
$i++;
}
}
}
$path[] = $dir;
foreach($path as $dirname){
$add = glob($dirname . '/*.css');
foreach($add as $file){
$code = file_get_contents($file);
$code_arab = arabicer($code);
file_put_contents($file,$code_arab);
}
}
When i start my script i found an waning message:
Warning: Invalid argument supplied for foreach() in /home/u274517531/public_html/libs/functions.php on line 131
I'm sure my array is not empty.
So, anyone can help me how to solve this problem ?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您说您确定数组不为空,但是 为 foreach() 提供的参数无效 错误消息意味着它甚至不是数组。如果您不相信,请自己尝试一下:
很可能是查找文件和 glob() 返回
FALSE
:You say you are sure that your array is not empty, but what the Invalid argument supplied for foreach() error message means is that it is not even an array. Try it youself if you don't believe it:
Most likely, there was an error finding files and glob() is returning
FALSE
:把这个:改成
这样:
我们不知道第131行是哪一行,所以我不知道哪一个foreach失败了。
(我猜是第二个,因为第一个实际上是通过
$path[] = ..
强制排列的)Change this:
to this:
We don't know which line 131 is, so I don't know which foreach fails.
(I'm guessing the 2nd, because the first is practically forced to array by
$path[] = ..
)我今天在 PHP 脚本中发现了同样的问题,需要从 foreach 循环切换到 for 循环才能修复它。不知道是什么意思,数组看起来和工作原理都是一样的。
I found the same issue with a PHP script today and it took switching from a foreach loop to a for loop to fix it. Not sure what means, the array looks and works the same.