Xweque
Xweque

Reputation: 643

Read file PHP not working

I am having some major troubles having my files read through PHP. This is not something I've found challenging in the past but now nothing seem to be working for me. The following is a small demo of what I'm trying to do.

if(!is_readable("test.txt"))
{
    echo "Cannot not read file!";
    return false;
}
echo file_get_contents("test.txt");
$fh = fopen("test.txt","r");
while ($line = fgets($fh))
{
    echo $line;
}

What could be wrong? All code above have been taken from tutorials/examples which is why I'm having such a hard time understanding what's the problem.

I've set this file to have all permissions, just in case, and I have my error reporting on.

The result of the above code is nothing. It will accept that the file is readable, it will not complain saying the file path is wrong when I use fopen yet it will not display the contents of the test.txt file (yes it holds content xD).

My main theory of the issue is that it may be a configuration error of some sort as the script happily download urls (eg http://*).

Upvotes: 0

Views: 204

Answers (1)

Robbo_UK
Robbo_UK

Reputation: 12149

Your code example does nothing when it is readable? What happens when you do this?

error_reporting(E_ALL);  // put this at top of script

if(!is_file("test.txt")){
    echo "Cannot not read file!";
}
else{
  echo 'is readable attempt to output contents';
  try {
      $fileContentAsString = file_get_contents("test.txt");
      var_dump($fileContentAsString);  // try this
      echo $fileContentAsString;
  }
  catch (Exception $e) {
      //echo $e->getMessage();
      echo "it failed for some reason.. view output";
      var_dump($e);  // try this
  }
}

Upvotes: 1

Related Questions