Page 1 of 1

newbie with php

Posted: Fri Nov 26, 2004 7:25 pm
by weeshdude
I trying to display the contents of every file in the directory called "news." All the files in news are .txt files. This is my code that I got so far. The problem is at it is reading the directory that index.php is in(www/u/username/htdocs/news) not the directory news. any help would appecatied.

$handle= "/www/u/username/htdocs/news";

if ($handle = opendir('.'))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
require($file);
echo "
" ;
echo "$file\n";
echo"

";
}
}
closedir($handle);
}
echo "finsh\n";
?>

Any help would be appecatied.

Aaron

ok I got that problem solved, but got another problem

Posted: Fri Nov 26, 2004 8:02 pm
by weeshdude
ok I want to display the contents of the file and it ain't doing it. It is displaying the files names right. Any help would be appecatied.

<?php

if ($handle = opendir('./news/'))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
readfile($file);
echo "\n"
echo "$file\n";
echo"\n"
}
}
closedir($handle);
}
echo "f\n";
?>

Posted: Fri Nov 26, 2004 8:04 pm
by hawleyjr
Take a look at [php_man]fopen[/php_man]

having problem with fopen()

Posted: Fri Nov 26, 2004 8:44 pm
by weeshdude
I'm opening the file, saving the file to a string then printing the string. Well when I echo the string (echo "$data"; ) it doesn't work. here is the code

$fp = fopen($file, "r");
$data = fread($fp, 1024*1024);
fclose($fp);
echo "$data";

any help would we appeacited.

Thanks

Aaron

Posted: Sat Nov 27, 2004 5:32 am
by Heavy
http://www.php.net/manual/en/function.f ... ntents.php
file_get_contents -- Reads entire file into a string
The manual is very useful.
Good luck.

Posted: Sat Nov 27, 2004 5:35 am
by Heavy

Code: Select all

string file_get_contents ( string filename &#1111;, bool use_include_path &#1111;, resource context &#1111;, int offset]]])
Means that all arguments to the function that are contained in [] are optional.
So this would be sufficient:

Code: Select all

<?php
$strFileContent = file_get_contents($strFileName);
?>

Thanks for the help problem solved.

Posted: Sat Nov 27, 2004 10:13 am
by weeshdude
Thanks for the help problem solved you guys are awesome