Jump to content

Listing Directory Files


White_Lily

Recommended Posts

Hi, I have a problem where my while loop isn't displaying what I have asked it to display, everything works except for this while loop and I can't see what the problem is code below:

 

while (false !== ($entry = readdir($handle)) && $fileGet = mysql_fetch_assoc($getfile)) {

if ($entry != "." && $entry != "..") {

$listFile = '<td>'.$entry.'</td>';

echo '<tr>';
echo '<td>'.$fileGet["file_name"].'</td>';
echo $listFile;
echo '<td>'.$fileGet["file_url"].'</td>';
echo '<td><a href="delete-file.php?id='.$fileGet["file_id"].'&file='.$fileGet["file_original"].'">Delete</a>
</td>';
echo '</tr>';


}
}

 

Any Ideas? Thanks - Lily

Link to comment
https://forums.phpfreaks.com/topic/271439-listing-directory-files/
Share on other sites

If it is not displaying what you asked it to display, what is it displaying. There's not a lot of room inside the loop for failure. What I do see is that you are retrieving an entry from the filesystem and an entry from the database at the start of each loop iteration. Then you are assuming that the two entries are for the same file or directory. However, since you do not process the current and parent directories ("." & "..") you are discarding two entries from the database (unless those two entries are in the database as well).

 

Personally, I would not depend on the two lists (readdir and the query) to return the same data in the same sequence. I would retrieve one set in an array and then loop through the other set matching to the array.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.