PHP's fopen is terminally failing

Posted by Skittles on Stack Overflow See other posts from Stack Overflow or by Skittles
Published on 2012-06-16T03:08:19Z Indexed on 2012/06/16 3:16 UTC
Read the original article Hit count: 188

Filed under:
|

Okay, I have GOT to be missing something totally rudimentary here. I have an extremely simple use of PHP's fopen function, but for some reason, it will not open the file no matter what I do.

The odd part about this is that I use fopen in another function in the same script and it's working perfectly. I'm using the fclose in both functions. So, I know it's not a matter of a rogue file handle.

I have confirmed the file's path and the existence of the target file also.

I'm running the script at the command-line as root, so I know it's not apache that's the cause. And since I am running the script as root, I am fairly confident that permissions are not the issue.

So, what on earth am I missing here?

function get_file_list() {
  $file = '/home/site/tmp/return_files_list.txt';
  $fp = fopen($file, 'r') or die("Could not open file: /home/site/tmp/return_files_list.txt for reading.\n");
  $files_list = array();
  while($line = fgets($fp)) {
    $files_list[] = $line;
  }
  fclose($fp);
  return $files_list;
}

function num_records_in_file($filename) {
  $fp = fopen( $filename, 'r' ); # or die("Could not open file: $filename\n");
  $counter = 0;

  if ($fp) {
    while (!feof( $fp )) {
      $line = fgets( $fp );
      $arr = explode( '|', $line );

      if (( ( $arr[0] != 'HDR' && $arr[0] != 'TRL' ) && $arr[0] != '' )) {
        ++$counter;
        continue;
      }
    }
  }

  fclose( $fp );
  return $counter;
}

As requested, here's both functions. The second function is passed an absolute path to the file. That is what I used to confirm that the file is there and that the path is correct.

© Stack Overflow or respective owner

Related posts about php

Related posts about fopen