Php 列出目录中的文件并构建html
我正在用一些flash游戏制作一个页面,但是,因为过去会有很多游戏,所以在网站上一个接一个地添加这些游戏会很无聊。我知道,使用PHP我们可以得到目录中的文件列表,我知道,使用这种方法,我可以将目录中的所有游戏显示到访问者页面,但是,我在想,你是否可以帮我在每个游戏中添加一个与游戏名相关的图像。例: 在“游戏”文件夹中,我们将有游戏:play1.swf、play2.swf、play3.swf 在“images”目录中,我们将有图像:play1.jpg、play2.jpg、play3.jpgPhp 列出目录中的文件并构建html,php,Php,我正在用一些flash游戏制作一个页面,但是,因为过去会有很多游戏,所以在网站上一个接一个地添加这些游戏会很无聊。我知道,使用PHP我们可以得到目录中的文件列表,我知道,使用这种方法,我可以将目录中的所有游戏显示到访问者页面,但是,我在想,你是否可以帮我在每个游戏中添加一个与游戏名相关的图像。例: 在“游戏”文件夹中,我们将有游戏:play1.swf、play2.swf、play3.swf 在“images”目录中,我们将有图像:play1.jpg、play2.jpg、play3.jpg 要显示
要显示游戏列表,我们可以将jpg文件与swf文件组合起来吗?Play1.jpg将是Play1.swf游戏的图像,当访问者点击图像时,将重定向到一个名为ex:playgames.php?Play1的页面
/foo/images
game1.jpg
game2.jpg
/foo/games
game1.swf
game2.swf
大概是这样的:
<?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>