zero-perfoliate
zero-perfoliate

Author Topic: Basic php help on $file=fopen is needed  (Read 1273 times)

Offline a-fit-of-pique

  • New PHP Members
  • Posts: 2
  • Karma: +0/-0
Basic php help on $file=fopen is needed
« on: December 01, 2008, 12:12:51 AM »
Hello, everybody!!!

I am sorry I am just a newbie, so my question will
sound probably silly to you.

I've just created a new account on one php-supporting
server, created my SQL database there, and uploaded
into the same directory these two files:

1) file's name: welcome.txt
    file's content:

     Will this file with these
     lines be opened by php code?



2) file's name: fileopen.php
    file's content:

    <html>
    <body>

    <?php
    $file=fopen("welcome.txt","r") or exit("Unable to open file!");
    ?>

    </body>
    </html>

As far as I understand, the php script in "fileopen.php"
should open my "welcome.txt" for reading, right? However,
when I click on the the file's link
( http://insights.freehyperspace5.com/exp/fileopen.php ),
my browser shows the full content of "fileopen.php", instead,
as  if it were an html file. Why is it so?
 
« Last Edit: December 01, 2008, 02:58:41 AM by a-fit-of-pique »

Offline PhPHelper

  • http://Digiscapers.com
  • Full Member
  • PHP Problem Solvers
  • ****
  • Posts: 179
  • Karma: +50/-0
    • Bad Apple Mail
Re: Basic php help on $file=fopen is needed
« Reply #1 on: December 16, 2008, 06:05:38 PM »
Code: [Select]
  $file=fopen("welcome.txt","r") or exit("Unable to open file!");
that only opens the file it is not doing anything else with it. If you want the file contents to be displayed you need a little more code.

Code: [Select]
//file name
$file = 'welcome.txt';
//open the file and read it's contents
$fo = fopen($file, 'r');
//get file contents and work out the file content size in bytes
$data = fread($fo, filesize($file));
//close the file
fclose($fo);

//to show the file contents echo out $data

Since your opening this txt file in a file that already had html in it will show both the html and the file contents.

Offline a-fit-of-pique

  • New PHP Members
  • Posts: 2
  • Karma: +0/-0
Re: Basic php help on $file=fopen is needed
« Reply #2 on: December 18, 2008, 09:18:50 AM »
WOW!!! Thank you very much!!!

Offline PhPHelper

  • http://Digiscapers.com
  • Full Member
  • PHP Problem Solvers
  • ****
  • Posts: 179
  • Karma: +50/-0
    • Bad Apple Mail
Re: Basic php help on $file=fopen is needed
« Reply #3 on: December 18, 2008, 09:30:43 AM »
Your most welcome  ;D

 

zero-perfoliate