开发者

List files on directory and build the html

I'm making a page with some flash games, but, since in the past will be many games it will be very bored to add all those games one by one on the site. I know that using PHP we can get the list of the files in the directory and I know that, using this method, I can display all the games on the directory to the visitors page but, I was thinking if you can hel开发者_如何学Pythonp me to add an image to each game that corespond with the name of the game. Ex:

In "games" folder we will have games: play1.swf, play2.swf, play3.swf and, in "images" directory we will have images: play1.jpg, play2.jpg, play3.jpg.

To display the list of the games, can we make a combination of jpg files with swf files? Play1.jpg will be the image of play1.swf game and, when a visitor clicks on the image, will be redirect to a page named ex: playgames.php?play1


assuming this directory structure:

/foo/images
     game1.jpg
     game2.jpg
/foo/games
     game1.swf
     game2.swf

Something like this:

<?php

$dir = "/foo";
$files = new DirectoryIterator($dir . DIRECTORY_SEPARATOR . 'games');
$games = array();

foreach($files as $file){

    if (!$file->isDot()) {
        $potential_image_location = $file->getPath() . '/../images/' . $file->getBasename('.swf') . '.jpg';

        if (file_exists($potential_image_location)) {
            $games[$file->getBaseName()] = array(
                'game_path' => $file->getPathname(),
                'image_path' => realpath($potential_image_location),
            );
        }
    }
}

if (!count($games)) {
   die("Sorry, couldn't find any game / image combos");
}
?>
<table>
<?php
foreach ($games as $game_name => $info) {
  echo '<tr><td><img src="' . htmlentities($info['image_path']) . '" alt="' . htmlentities($game_name) . '"></td>' . PHP_EOL;
  echo '<td>' . $info['game_path'] . '</td></tr>';
}
?>

</table>


its all about using opendir in a clever way, heres the docs on it http://php.net/manual/en/function.opendir.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜