-1

I'm trying to open a simple text file on my raspberry pi using PhP. This is the code:

<html>
<body>

<?php
$myfile = fopen("/home/pi/Desktop/hello.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("/home/pi/Desktop/hello.txt"));

fclose($myfile);

</body>
</html>

I saved the above code as .php and opened it on the web browser by typing the raspberry pi IP address and the file name i.e. 192.168.0.17/hi.php yet the web browser is saying "Not Found". I also installed sudo apt-get install apache2 php5 , sudo apt-get install lighttpd php5, sudo apt-get install git-core, and cloned the Wiring PI.

Can some one tell me if I'm doing anything wrong please? Thanks

0

You probably just have your hi.php in the wrong directory. Whatever http server you are using will have a document root directory (defined in its config file). This is where it will look for files when parsing the URL that you type in your request, for example, if document root = /var/www and the client browser types http://192.168.0.17/mydir/hi.php then it will look for /var/www/mydir/hi.php.

Normally your http server will come with a little test file, index.html, index.php that it will display as an installation verification test. Find out where that file resides and put your hi.php in same dir.

Some http servers have a special directory they want you to put php files in. I usually use lighttpd and IIRC it has one but does not force me to use it.

And, by all means, make sure you http server is running before you try to connect to it!

0

I agree with MartinMarty in my experience using apache..the original file by default was /var/www/html. After downloading PHP I had to change the HTML to PHP. I did this by typing

Sudo nano /var/www/php5 I then added some simple PHP code to show the date saved the file usomg control x Y Enter Then from the terminal I had to remove /var/www/HTMl I then type ls to verify it is gone revisit the ip address hit refresh and you should see the date and the file will be php

I hope this helps..it worked for me

0

Martin Marty is right, make sure that you placed your file in the right directory (/var/www/html/ is the usual root with Apache2, place all your website here). However with Apache2 you shouldn't have any problem with special php file

But you also forgot to close your php code and to add a (big errors often came because I didn't have a clean code so if it can help):

<!DOCTYPE html>
<html>
   <head>
       <title>Opening Doc</title>
       <meta charset="utf-8"/>
   </head>

   <body>
      <?php
          $myfile = fopen("/home/pi/Desktop/hello.txt", "r") or die("Unable to open file!");
          echo fread($myfile,filesize("/home/pi/Desktop/hello.txt"));

          fclose($myfile);
      ?>
   </body>
</html>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.