Search code examples
phpfile-get-contents

PHP get_file_path returning false


I'm trying to read in a file contents as follows, but I'm getting false back.

I looked at get_file_contents returns false but their solution isn't working.

$fileName = "/opt/IB/cust/HW/junk/Exclude_E7.csv";
$hw= "EPHRPH1";
print "checking for " . $hw. "\n";
if(strlen($hw)>6)
{
    //look for hw in file
    $contents = file_get_contents($fileName);
    var_dump($contents);
}

prints

checking for EPHRPH1
bool(false)

When I list the file at the command line, it finds it. I've tried this with exec and grep, and grep can't find the file or dir with the full path. I looked at the file, and it has 777 permissions, and the same user as I'm running the script with.

I tried this as well, like at the link, but it's returning false as well.

$contents = file_get_contents(__DIR__ . $fileName);

Any idea why it's not finding/reading the file or how to get the file contents read into a string? My ultimate goal is to look for $hw in the file contents. I'm debugging why this doesn't work, and found out it's not finding the file:

$result = strpos(file_get_contents(__DIR__ . $fileName),$hw);

Solution

  • I worked with the error message I got by turning on logging as suggested above, and changed it to this:

    $contents = file_get_contents(__DIR__ . '/' . "junk/Exclude_E7.csv"); 
    

    and it worked. Thanks for the help!