/    Sign up×
Community /Pin to ProfileBookmark

List files on directory and build the php/html page

Hello there!
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 help me to add an image to each game that correspond 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
Can someone help me on this? It will be a big help for my project.
Thank you very much and waiting for your reply!

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 14.2011 — I might start with something like this (untested):
[code=php]
<ul class='video_list'>
<?php
$videoDir = "relative/path/to/videos/";
$imageDir = "relative/path/to/images/";
$videos = glob($videoDir . "*.swf");
foreach($videos as $vid) {
$info = pathinfo($vid);
if(file_exists($imageDir . $info['basename'] . ".jpg")) {
$target = sprintf(
"<img src='&#37;s.jpg' alt='%s'>",
htmlspecialchars($imageDir . $info['filename'] . ".jpg"),
htmlspecialchars($info['basename'])
);
}
else {
$target = htmlspecialchars($info['basename']);
}
printf(
"<li><a href='%s'>%s</a></li>n",
htmlspecialchars($vid),
$target
);
}
?>
</ul>
[/code]

You could build upon that by creating an object-oriented class[es] or such, but that should give you plenty to work with.
Copy linkTweet thisAlerts:
@KleidiauthorFeb 14.2011 — I might start with something like this (untested):
[code=php]
<ul class='video_list'>
<?php
$videoDir = "relative/path/to/videos/";
$imageDir = "relative/path/to/images/";
$videos = glob($videoDir . "*.swf");
foreach($videos as $vid) {
$info = pathinfo($vid);
if(file_exists($imageDir . $info['basename'] . ".jpg")) {
$target = sprintf(
"<img src='%s.jpg' alt='%s'>",
htmlspecialchars($imageDir . $info['filename'] . ".jpg"),
htmlspecialchars($info['basename'])
);
}
else {
$target = htmlspecialchars($info['basename']);
}
printf(
"<li><a href='%s'>%s</a></li>n",
htmlspecialchars($vid),
$target
);
}
?>
</ul>
[/code]

You could build upon that by creating an object-oriented class[es] or such, but that should give you plenty to work with.[/QUOTE]


Thank you NogDog. It's great. The script works, lists all the games but the links and images does not appear ? I have sett the "videoDir=/var/www/vhosts/username/httpdocs/games/Sports/" but it's not displaying any image and the link is not working. The HTML generated looks like this:
[code=html]<ul class='video_list'>
<li><a href='/var/www/vhosts/username/httpdocs/games/Sports/3d.swf'>3d.swf</a></li>
<li><a href='/var/www/vhosts/username/httpdocs/games/Sports/9m_soccer.swf'>9m_soccer.swf</a></li>
<li><a href='/var/www/vhosts/username/httpdocs/games/Sports/AnnaTennis.swf'>AnnaTennis.swf</a></li>
<li><a href='/var/www/vhosts/username/httpdocs/games/Sports/BOWLING-2.swf'>BOWLING-2.swf</a></li>
<li><a href='/var/www/vhosts/username/httpdocs/games/Sports/BasketWild.swf'>BasketWild.swf</a></li>
<li><a href='/var/www/vhosts/username/httpdocs/games/Sports/airhockey.swf'>airhockey.swf</a></li>
<li><a href='/var/www/vhosts/username/httpdocs/games/Sports/balibaloo.swf'>balibaloo.swf</a></li>
<li><a href='/var/www/vhosts/username/httpdocs/games/Sports/baseball.swf'>baseball.swf</a></li>
<li><a href='/var/www/vhosts/username/httpdocs/games/Sports/basket.swf'>basket.swf</a></li>
<li><a href='/var/www/vhosts/username/httpdocs/games/Sports/basketball_shooting.swf'>basketball_shooting.swf</a></li>
</ul>[/code]

When a visitor clicks on it, i suppose that should appear a page where the game is embeded ... how should it be done?

I was thinking to made something else now, whith the script you posted here; to make a section in admin page where all Games to be listed in a dropdown menu (images too) and where the admin can select the game and it's image, put an User firendly name and a permalink for it. All this details will be stored in db, and then to loop in the game page. This option is an intermediary one, if the first request will be to complicated for me.

As an expert, what you suggest?

Thanks again for your help!
Copy linkTweet thisAlerts:
@NogDogFeb 14.2011 — You don't want an absolute path to the directory, since HTTP requests will be ignorant of the file system. Instead, use a path relative to the script's directory, e.g. "games/Sports/" if the script is in the web root directory, or maybe something like "../games/Sports/" if it's in a separate directory just under the web root.
×

Success!

Help @Kleidi spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...