0

Hi,

i am trying to open a file in php but its not.

1. i saved a txt file, called welcome.txt
welcome.txt content

"welcome to PHP file"

2. then i wrote another file, "file.php".
content is:

<html>
<body>

<?php
$file=fopen("welcome.txt","r");
?>

</body>
</html>

3. "file.php" and "welcome.txt" on the same directory.

4. i went to browser and typed URL/file.php
but i get blank screen.
will i supposed to see the content of welcome.txt in the browser screen?

Thx.

Edited by Ezzaral: Tired of adding tags to your posts.

4
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by ddymacek
0

Do you have a local server installed on your machine? PHP won't run without it. If you don't know what that is click here.

0

You're only opening the file. You're not outputting it.

Try changing

$file=fopen("welcome.txt","r");

to

readfile("welcome.txt");
0

either try file_get_contents or properly read your file.
both

// assuming welcome.txt is in the same directory.
$text = file_get_contents("welcome.txt");
echo $text;
// also you are only opening your file and not doing anything with it.
$myFile = "welcome.txt";  // set your file name & path
$fh = fopen($myFile, 'r');  // open a file (this is usually called a handle)
$thedata = fread($fh, filesize($myFile));  // read everyline in the file into variable $thedata.
fclose($fh);  // close your file handle.
echo $thedata;  // echo retrieved contents of welcome.txt file.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.