Reputation: 2918
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.
Upvotes: 1
Views: 168
Reputation: 2918
Wow! Well, I figured it out. On a whim, I decided to try trimming the file name. Apparently, it was carrying some whitespace or something at the end of the filename. So, when it tried to open the file, it couldn't due to looking for $filename +
Learn something new everyday, I guess.
Upvotes: 2