php file()返回boolean true而不是文件内容数组

Bit of strange one here.

This works locally but when run on the staging server causes the code to fail because the file() function is returning a boolean true rather than array of records

Code excerpt:

if ( $input_list = file("{$path}/input_list.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) !== false) {
     foreach ($input_list as $row) {
          ... etc...
            }
}

The file exists (I access the dir/file via DirectoryIterator), it has content. The user executing the script has read & write permission to the folder and file.

if I echo a gettype($input_list) it returns: bool(true)

edit - I have also tried with out the different flags - FILE_IGNORE_NEW_LINES and FILE_SKIP_EMPTY_LINES

Any ideas why it works locally (populates an array with records) but returns true on the server.

Server is php 5.3.21 TIA PG

The reason it is doing this is because of the !== false.

You are first assigning the result of file(..) to $input_file, then checking if it is not equal to false. This will return true if the file exists.

So, remove the !== false so you have:

if($input_file = file("{$path}/test.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)) {
    print_r($input_file);
} else {
    echo 'nope';
}

You must use parentheses:

if(($input_list = file(…)) !== false) {

Otherwise, your condition would be interpreted as:

if($input_list = (file(…) !== FALSE)) {