Merge pull request #58 from lainchan/57-priority-banners
Priority Banners (issue #57)
This commit is contained in:
commit
0678b74b7a
58
b.php
58
b.php
@ -1,21 +1,47 @@
|
|||||||
<?php
|
<?php
|
||||||
//renaming to b.php
|
// This script assumes there is at least one normal (non-priority)
|
||||||
$dir = "banners/";
|
// banner!
|
||||||
$files = scandir($dir);
|
|
||||||
$images = array_diff($files, array('.', '..'));
|
|
||||||
$name = $images[array_rand($images)];
|
|
||||||
|
|
||||||
// snags the extension
|
// Get the files in a directory, returns null if the directory does
|
||||||
$img_extension = pathinfo($name, PATHINFO_EXTENSION);
|
// not exist.
|
||||||
|
function getFilesInDirectory($dir) {
|
||||||
|
if (! is_dir($dir)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
// send the right headers
|
return array_diff(scandir($dir), array('.', '..'));
|
||||||
header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1
|
}
|
||||||
header('Pragma: no-cache'); // HTTP 1.0
|
|
||||||
header('Expires: 0'); // Proxies
|
|
||||||
header("Content-type: image/" . $img_extension);
|
|
||||||
header("Content-Disposition: inline; filename=" . $name);
|
|
||||||
|
|
||||||
// readfile displays the image, passthru seems to spits stream.
|
// Serve a random banner and exit.
|
||||||
readfile($dir.$name);
|
function serveRandomBanner($dir, $files) {
|
||||||
exit;
|
$name = $files[array_rand($files)];
|
||||||
|
|
||||||
|
// snags the extension
|
||||||
|
$ext = pathinfo($name, PATHINFO_EXTENSION);
|
||||||
|
|
||||||
|
// send the right headers
|
||||||
|
header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1
|
||||||
|
header('Pragma: no-cache'); // HTTP 1.0
|
||||||
|
header('Expires: 0'); // Proxies
|
||||||
|
header("Content-type: image/" . ext);
|
||||||
|
header("Content-Disposition: inline; filename=" . $name);
|
||||||
|
|
||||||
|
// readfile displays the image, passthru seems to spits stream.
|
||||||
|
readfile($dir.$name);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get all the banners
|
||||||
|
$bannerDir = "banners/";
|
||||||
|
$priorityDir = "banners_priority/";
|
||||||
|
|
||||||
|
$banners = getFilesInDirectory($bannerDir);
|
||||||
|
$priority = getFilesInDirectory($priorityDir);
|
||||||
|
|
||||||
|
// If there are priority banners, serve 1/3rd of the time.
|
||||||
|
if($priority !== null && count($priority) !== 0 && rand(0,2) === 0) {
|
||||||
|
serveRandomBanner($priorityDir, $priority);
|
||||||
|
}
|
||||||
|
|
||||||
|
serveRandomBanner($bannerDir, $banners);
|
||||||
?>
|
?>
|
||||||
|
15
banners.php
15
banners.php
@ -4,14 +4,19 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<?php
|
<?php
|
||||||
if ($handle = opendir('banners')) {
|
function listBannersInDir($dir) {
|
||||||
while (false !== ($entry = readdir($handle))) {
|
if ($handle = opendir($dir)) {
|
||||||
if ($entry != "." && $entry != "..") {
|
while (false !== ($entry = readdir($handle))) {
|
||||||
echo "<a href=\"banners/$entry\"><img src=\"banners/$entry\" alt=\"$entry\" style=\"width:348px;height:128px\"></a> ";
|
if ($entry != "." && $entry != "..") {
|
||||||
|
echo "<a href=\"$dir/$entry\"><img src=\"$dir/$entry\" alt=\"$entry\" style=\"width:348px;height:128px\"></a> ";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
closedir($handle);
|
||||||
}
|
}
|
||||||
closedir($handle);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
listBannersInDir("banners_priority");
|
||||||
|
listBannersInDir("banners");
|
||||||
?>
|
?>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
Loading…
Reference in New Issue
Block a user