note 101719 added to function.readdir
| From: | tomfc at uninova dot pt | Date: | Thu, 06 Jan 2011 17:11:25 +0000 |
| Subject: | note 101719 added to function.readdir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-175501@lists.php.net to get a copy of this message | ||
an example, just for fun, including and playing a random file within a webpage:
<?php
/**
* get the number of files within a given dir
*/
function fileCounter($dir){
$counter = 0;
if ($handle = opendir($dir)) {
//echo "Directory handle: $handle\n";
//echo "Files:\n";
/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
//echo "<BR>".$counter." - $file";
$counter++;
}
closedir($handle);
}
$counter -= 1; // in order to exclude '.' and '..', as well as start the
counter on 1
return $counter;
}
/**
* get the filename in a giver dir, starting the first file with the index 1
*/
function fileNameByIndex($dir, $fileIndex){
$counter = 0;
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
$counter++;
if($counter - 2 == $fileIndex)
return $file;
}
closedir($handle);
}
return "";
}
$nFiles = fileCounter("sounds");
$rndIndex = rand(1, $nFiles);
$fileName = fileNameByIndex("sounds", $rndIndex);
$player_obj = "<embed type='application/x-mplayer2' pluginspage='http://www.microsoft.com/Windows/MediaPlayer/'
name='mediaplayer1' ShowStatusBar='false' EnableContextMenu='false'
autostart='true' width='1' height='1' loop='false'
src='sounds/$fileName' />";
echo $player_obj;
?>
----
Server IP: 69.147.83.197
Probable Submitter: 193.137.127.56
----
Manual Page -- http://www.php.net/manual/en/function.readdir.php
Edit -- https://master.php.net/note/edit/101719
Del: integrated -- https://master.php.net/note/delete/101719/integrated
Del: useless -- https://master.php.net/note/delete/101719/useless
Del: bad code -- https://master.php.net/note/delete/101719/bad+code
Del: spam -- https://master.php.net/note/delete/101719/spam
Del: non-english -- https://master.php.net/note/delete/101719/non-english
Del: in docs -- https://master.php.net/note/delete/101719/in+docs
Del: other reasons-- https://master.php.net/note/delete/101719
Reject -- https://master.php.net/note/reject/101719
Search -- https://master.php.net/manage/user-notes.php