Page 1 of 1

file_put_contents problem

Posted: Mon Nov 12, 2007 3:22 pm
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...

Posted: Mon Nov 12, 2007 3:29 pm
by alex.barylski
Check to make sure your loop is actually iterating files and if the paths are correct your code should work.

Posted: Mon Nov 12, 2007 3:52 pm
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);

?>

Posted: Mon Nov 12, 2007 3:56 pm
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.