-2

When I embed the php code into the html file to read from the file in the same directory it does't display anything on the page.

  <html>
  <head>
   <title>Reading from text files</title>
  </head>
  <body>
   <?php
   $f = fopen("blah.txt", "r");
   echo fgets($f);                                      
   fclose($f); 
   ?>
 </body>
 </html>
Sjon
  • 4,989
  • 6
  • 28
  • 46
user3293692
  • 567
  • 1
  • 5
  • 14

1 Answers1

1

Shown in your comments, I'm guessing you need to install the PHP server first.

The simplest way is to goto this page and install XAMPP for your operating system as I'm assuming Windows.

When installed and all the lights are green, or at least the PHP server open up your browser and goto http://localhost as this should open up a welcome page.

Using XAMPP your web root directory should be located in C:/xampp/htdocs/ and placing your files there with an index.php and your blah.txt and it should execute. Because index.php is the defauled opened file refreshing http://localhost would remove the welcome page and load up your script.

However perhaps this code is simpler to use instead of fopen:

<?php
    if(file_exists($fname = './blah.txt')){
        echo file_get_contents($fname);
    } else {
        echo 'I should have known to set the correct path..';
    }
?>
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Xorifelse
  • 7,878
  • 1
  • 27
  • 38
  • 1
    that isn't what the question is about. edit: this comment as per your original answer http://stackoverflow.com/revisions/33945272/1 – Funk Forty Niner Nov 26 '15 at 18:46
  • Well, correct me if i'm wrong but if it isn't showing anything on the page i'm guessing PHP error reporting is turned off and the file cannot be opened showing nothing. Otherwise PHP code will be shown in the html page. Edit: never mind, it should still throw a warning of some sort. – Xorifelse Nov 26 '15 at 18:50
  • 1
    OP's comment up there: *"yes into the `.html` file"*. Won't get much of an error with PHP with that ;-) Not till they change it to `.php` or instruct Apache to treat those as PHP, if... they have a webserver/PHP installed that is. – Funk Forty Niner Nov 26 '15 at 18:54
  • Fully edited my post, thanks for making me aware. – Xorifelse Nov 26 '15 at 18:59