Tuesday, July 28, 2009

How to read article from text file ?

Contact Us :
Hire PHP Web Developer

# problem : How to read article from text file ?
# article txt file will be in following way
# root_directory/test/import/file1.txt , root_directory/test/import/file2.txt
# filename or directory does not matter
# Article format : 1st line = title , rest of will be article body

$dir = "test/import";

if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$files[] = $file;
}
}
closedir($handle);
}

for($i=0; $i<count($files); $i++)
{
$file = $dir."/".$files[$i];
$fp = fopen($file, "r");
$title = fgets($fp);
$body = fread ($fp, filesize ($file));
fclose ($fp);

echo "<h2>".$title."</h2>";
echo "<span>".nl2br($body)."</span>";
echo "<br>";
}