0

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("\n", 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("\n", fread($fp1, filesize($filename1))); 
} 

$num = 1;

foreach($array1 as $val1)
{
foreach($array as $val)
{
if(strstr($val1, $val)) 
{
print_r('\n'); //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?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
JPro
  • 6,292
  • 13
  • 57
  • 83
  • Related: [Invalid argument supplied for foreach()](http://stackoverflow.com/questions/2630013/invalid-argument-supplied-for-foreach) – hakre Jan 27 '13 at 13:14

4 Answers4

1

Your fopen calls are not finding their file, I imagine. First, remove the '@' from '@fopen', so you can see it fail. Then, do this:

$filename = dirname(__FILE__).'/search_items.txt';
//...
$filename1 = dirname(__FILE__).'/file1.log';

That will keep your file locations straight.

Derek Illchuk
  • 5,638
  • 1
  • 29
  • 29
  • the search_items.txt is located in a shared drive. and the php file is in htdocs folder. i guess dirname(__FILE__) searches for the file in its current directory? – JPro Dec 16 '09 at 14:49
  • You need to specify the full path, however you do so. `dirname` is great when your target file is located relative to your script file, it was a guess on my part. – Derek Illchuk Dec 16 '09 at 14:52
  • I put the files in the same path as of the script and they are working fine now. – JPro Dec 16 '09 at 15:02
0

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.

Scott Saunders
  • 29,840
  • 14
  • 57
  • 64
0

Maybe variables are empty or not exist?

$array = $array1 = array();

//...

foreach((array)$array1 as $val1)
{
    foreach((array)$array as $val)
    {
        if(strstr($val1, $val)) 
        {
            echo 'line : '.$num.'->'.$val1.'<br>';
        }
    }
    $num++;
}
merkushin
  • 481
  • 9
  • 17
0

To be on the safe side you should add a check if the file pointers are valid before running the foreach loops, or throw some errors if you fail to open a file.

<?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("\n", fread($fp1, filesize($filename1))); 
} 

$num = 1;

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

    }
}

?>

Keep in mind that when running a script from CLI, the current directory is the directory from which the script was started. When running trough Apache, the current directory is the directory of the script. This bit me a couple of times.

Daniel Dinu
  • 1,783
  • 12
  • 16