tags:

views:

35

answers:

2

hello guys,

i wonder what i'm doing wrong! i want to read a folder and run through the existing files, checking if they are either images or textfiles.

if there are textfiles they should be put into a div, if there are images the should be output as an image.

<?php
$path = 'thumbs';
if ($handle = opendir($path)) {
    while (false !== ($file = readdir($handle))) {
        $ext = pathinfo($file, PATHINFO_EXTENSION);
            if ($ext == "jpg" || $ext == "jpeg" || $ext == "gif" || $ext == "png") {
                print "<img class='thumb' src='$path/$file'/>";
            } else if ($ext == "txt" || $ext == "rtf") {
                //read text
                $lines = file_get_contents($file);
                $lines = str_replace("\n","<br/>",$lines);
                print "<div class='text'>" . $lines . "</div>";
                //read text
            }
    }
    closedir($handle);
}
?>

there seems to be a problem i can't find, because ALL IMAGES get put out, however only ONE of a few textfiles gets printed. Any ideas why it only prints out one textfile???

thank you for your help!

A: 

Are you sure there are more than one txt or rtf files in thumbs directory?

Adam
yes, there are 4 txt files and 5 images. all images get printed the way i want them, however only one textfile (test_one.txt - with some lorem ipsum in it) gets read.
A: 

Depending on where this is being run from it may not know the path to the file...try something like this:

$lines = file_get_contents($path . '/' . $file);  
Jack
thank you! that's it!