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.

Recommended Answers

All 4 Replies

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.

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

Try changing

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

to

readfile("welcome.txt");

I did it and no luck.

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.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.