newbie with php

This forum is for discussing the future of the PHP Developer's Network, as well as for us to get your opinion on things.

This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
weeshdude
Forum Newbie
Posts: 7
Joined: Fri Nov 26, 2004 7:19 pm

newbie with php

Post 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
weeshdude
Forum Newbie
Posts: 7
Joined: Fri Nov 26, 2004 7:19 pm

ok I got that problem solved, but got another problem

Post 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";
?>
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Take a look at [php_man]fopen[/php_man]
weeshdude
Forum Newbie
Posts: 7
Joined: Fri Nov 26, 2004 7:19 pm

having problem with fopen()

Post 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
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Post 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.
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Post 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);
?>
weeshdude
Forum Newbie
Posts: 7
Joined: Fri Nov 26, 2004 7:19 pm

Thanks for the help problem solved.

Post by weeshdude »

Thanks for the help problem solved you guys are awesome
Post Reply