JapSeyz
JapSeyz

Reputation: 35

Display image and file in a folder

I would like to display the content of a folder, with an image and the file name next to it. I have tried to get the file name via scandir, but it would not change line, just out in 1 long line.

What i really want is something like this:
[Image here] [name here]

code:

$directory = '../../Images'; 
$files = scandir($directory);
print_r($files);

Upvotes: 0

Views: 1277

Answers (2)

noetix
noetix

Reputation: 4923

Assuming that every file in the directory is an image:

<?php

$directory = './images';

foreach (new DirectoryIterator($directory) as $fn)
{
    if (!$fn->isDir() && !$fn->isDot())
    {
        printf("<p><img src=\"./images/%1\$s\" /> %1\$s</p>\n", $fn->getFilename());
    }
}

Upvotes: 1

C. E.
C. E.

Reputation: 10607

Table would work. But you have to surround it with <html> <body> and so on if you want it to render properly.

$root = 'http://example.com/images/';
$files = scandir( $directory );
echo '<table>';
foreach( $files as $file )
{
    if(is_file($file))
    {
        echo '<tr><td><img src="' . $root . $file . '" /></td><td>' . $file . '</td></tr>';
    }
}
echo '</table>

Upvotes: 0

Related Questions