dougongyou7364 2009-12-16 14:39
浏览 31
已采纳

在命令行中的php中使用无效的参数

I wrote a script in php which reads two files and takes all the strings from one file and searches them in other file. This is working fine in web browser. But when I try to run it through command line, it says 'invalid arguments supplied for foreach() at line....' am I missing anything?

<?php
$filename = 'search_items.txt'; 
$fp = @fopen($filename, 'r'); 
if ($fp) { 
 $array = explode(",", fread($fp, filesize($filename))); 
} 

$filename1 = 'file1.log'; 
$fp1 = @fopen($filename1, 'r'); 
if ($fp1) { 
 $array1 = explode("
", fread($fp1, filesize($filename1))); 
} 

$num = 1;

foreach($array1 as $val1){
 foreach($array as $val){
  if(strstr($val1, $val)){
   echo 'line : '.$num.'->'.$val1.'<br>';
  }
 }
++$num;    
}    
?>

<?php





$filename = 'search_items.txt'; 
$fp = fopen($filename, 'r'); 
if ($fp) { 
 $array = explode(",", fread($fp, filesize($filename))); 
} 

$filename1 = 'file1.log'; 
$fp1 = fopen($filename1, 'r'); 
if ($fp1) { 
 $array1 = explode("
", fread($fp1, filesize($filename1))); 
} 

$num = 1;

foreach($array1 as $val1)
{
foreach($array as $val)
{
if(strstr($val1, $val)) 
{
print_r('
'); //2
}
}
++$num;
print_r($val1); // 1
}

Ok, the script is running now, but with something funny going on. if I remove the print in comment 1 and place it in comment 2 place, the results I am getting is the last result , i.e just one last result. not the full searches. Can anyone tell me why?

  • 写回答

4条回答 默认 最新

  • dongyudun6465 2009-12-16 14:48
    关注

    Probably your file paths are off, so $array and $array1 are never created. Relative paths will be from where you call the script, not the location of the script.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 VUE项目怎么运行,系统打不开