0

This works on my test environment, but on my live server there is a later version of PHP which is throwing up an error and breaking my program

The code is

$oldFile = fopen("D:/ftpfolderreport/report/" . $last_file, "r");
while(!feof($oldFile))
{
$buffler = fgets($oldFile);
$bufflerArray = explode(",", $buffler);
$key = $bufflerArray[0];
$oldFileArray[$key] = $bufflerArray[1];
}
fclose($oldFile);

This line:

$oldFileArray[$key] = $bufflerArray[1];

Is throwing out this error

Notice: Undefined offset: 1 in D:\apps\wamp\www\Compliance2\compareFtpReports.php on line 57

I think this is to do with how I'm adding the $key variable inside the argument. I've tried it as ["$key"] and ['$key'] but it doesn't like it.

I have tried defining the key variable earlier in the program but still doesn't like it. I've been searching around online but can't find anything of help. Anyone any ideas?

Thanks, Stephen.

6
  • please show us a line or 2 of your file so we understand what is happening here. Commented Nov 4, 2014 at 12:25
  • 1
    print_r the $buffler before exploding the string, to see where it stumbles. A trailing empty line is most likely. Btw, there is also a file() function or SplFileObject for reading lines. Commented Nov 4, 2014 at 12:25
  • 1
    It means there is no element with the key 1 in the array $bufflerArray; Commented Nov 4, 2014 at 12:26
  • use if(isset($bufflerArray[1])) { $oldFileArray[$key] = $bufflerArray[1]; } Commented Nov 4, 2014 at 12:27
  • $bufflerArray = explode(",", $buffler); if(count($bufflerArray)<2) continue; Commented Nov 4, 2014 at 12:28

1 Answer 1

1

add checks for empty

if (!empty($bufflerArray[1])) {
   $key = $bufflerArray[0];
   $oldFileArray[$key] = $bufflerArray[1];
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.