Search code examples
phpfgets

PHP fgets() not recognizing <?php


I am trying to parse the first few lines of a file however I just noticed, if the first line of the file is "<?php" it won't echo. However, if the first line of the file is "test" it will. Any ideas as to why?

<?php
$file= fopen("testfile.php", "r") or die("Unable to open file!");
echo fgets($file);
fclose($file);  
?>

Solution

  • I assume you use a default content type text/html, hence <?php is recognised as an HTML tag and your browser doesn't display it. Check the source code of your page.

    Here is a nice presentation how to do it: http://www.wikihow.com/View-Source-Code.