Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I'm just learning PHP here. I have a page in PHP. On other side, I have place a welcome.txt on same running folder. I want to read all line of code include the breakline and display on table but I got error. Below are my code in PHP:

PHP
<td width="600" bgcolor="#213A3F">
<?php
$file = "/content/welcome.txt";
$f = fopen($file, "r");
while ( $line = fgets($f, 1000) ) {
print $line;
}
?>
</td>



But and error display when I running my page:

VB
Warning: fopen(/content/welcome.txt): failed to open stream: No such file or directory in C:\xampp\htdocs\solaire\home.php on line 34

Warning: fgets() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\solaire\home.php on line 35


Can someone please find my problem here and tell me what I've done wrong? I need guidance.

Also, I appreciate if someone can give me link website for PHP code, CSS and something like learning website development.
Posted

Assuming your content folder is in the same directory as your php file, then:
PHP
<?php
$file = "./content/welcome.txt";
$f = fopen($file, "r") or exit("Unable to open file!");
// read file line by line until the end of file (feof)
while(!feof($f))
{
  echo fgets($f)."<br />";
}

fclose($f);
?>

Visit w3schools[^] to find out about web development.
 
Share this answer
 
v2
Comments
Luiey Ichigo 13-Jan-14 2:08am    
Hi peter..thank you sir..u are awesome. This code works.many thanks for the solution and for the link :)
Consider that content folder is in the same directory as your php file, then:

PHP
$myfile = fopen("file.txt", "r") or die("Unable to open file!");

echo "<table>";
while(!feof($myfile)) {
  echo "<tr><td width='600' bgcolor='#213A3F'>";
  echo fgets($myfile) . "<br>";
  echo "</td></tr>";
}
echo "</table>";
fclose($myfile);


Hope for better coding :)
 
Share this answer
 
just change the path to where the file is located
for example
my php file and txt file located in the same directory
This script will return the each line as row in the table
<?php

$file="file.txt" ;
$f=fopen($file,"r");
while($line=fgets($f))
{
echo "";
}
?>
$line
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900