hardik m hardik m - 1 year ago 74
PHP Question

Reading all file contents from a directory - php

This is actually is an easy work
i want to display contents of all files located in specified folder.

i am passing directory name

echo "<a href='see.php?qname=". $_name ."'>" . $row["qname"] . "</a>";

on second page ,

i am writing

while($entryname = readdir($myDirectory))
if($entryname=="." || $entryname==".." )
$fileHandle=fopen($entryname, "r");

while (!feof($fileHandle) ) {
$line = fgets($fileHandle);
echo $line . "<br />";


but i am unable to read any file ,
i have changed their permissions too

i tried putting directory name statically and it works,
any solution?

Answer Source

$entryname will contain JUST the filename, with no path information. You have to manually rebuild the path yourself. e.g.

$dh = opendir('/path/you/want/to/read/');
while($file = readdir($dh)) {
    $contents = file_get_contents('/path/you/want/to/read/' . $file);
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^---include path here

Without the explicit path in your "read the file code", you're trying to open and read a file in the script's current working directory, not the director you're reading the filenames from.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download