file_put_contents problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

file_put_contents problem

Post by Sindarin »

Code: Select all

<?

$dirpath = "/home/user456/public_html/test/upload";
$dh = opendir($dirpath);
while (false !== ($file = readdir($dh))) {
$content=file_get_contents($file);
echo "$content<br><br>";
}
closedir($dh);

?>
I am trying to list the contents of some text files in my upload directory, but I must be doing something wrong with file_get_contents...
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Check to make sure your loop is actually iterating files and if the paths are correct your code should work.
User avatar
seppo0010
Forum Commoner
Posts: 47
Joined: Wed Oct 24, 2007 4:13 pm
Location: Buenos Aires, Argentina

Post by seppo0010 »

I think you are missing the directory path to the file... so PHP can't find it

Code: Select all

<?php

$dirpath = "/home/user456/public_html/test/upload";
$dh = opendir($dirpath);
while (false !== ($file = readdir($dh))) {
$content=file_get_contents($dirpath . $file);
echo "$content<br><br>";
}
closedir($dh);

?>
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Post by Sindarin »

Code: Select all

<?
$dirpath = "/home/user456/public_html/test/upload"; 
$dh = opendir($dirpath);
while (false !== ($file = readdir($dh))) {
//
if (!is_dir("$dirpath/$file")) {
$filename="$dirpath/$file";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
//
echo "$contents<br><br>";

}
}
closedir($dh);

?>
I tested with fread and it works.

How come though it lists only the files I uploaded from now on?
There were some older txt files that did not get listed...odd.
Post Reply