Log in

View Full Version : Directory Display.



Jeffreyv1987
09-08-2009, 06:56 PM
Alright ive got a code to display my directory which has 20 files in.
Is there a way to only display 5 of the files or maybe display 5 random files..


<?php
$directory = "./files";
//Directory is set;

$dir = opendir($directory);
//Opened the directory;

while($file = readdir($dir)){

//Loops through all the files/directories in our directory;
if($file!="." && $file != ".." ){

echo '<A href=" ' . $file .' "> ' . $file .' </a> </br>';

}
}
?>

This is the script ive got so far. im having trouble..

JShor
09-08-2009, 07:35 PM
You can put the files into an array, then randomize it. Then, use array_slice() to limit the results.



<?php
$directory = "./files";
$arr = array();
//Directory is set;

$dir = opendir($directory);
//Opened the directory;

while($file = readdir($dir)){

//Loops through all the files/directories in our directory;
if($file!="." && $file != ".." ){

array_push($arr, $file);

}
}

shuffle($arr);

$args = array_slice($arr, 0, 5);

foreach($args as $a) {

echo '<A href=" ' . $a .' "> ' . $a .' </a></br>';


}
?>


HTH:)

JasonDFR
09-08-2009, 08:46 PM
function isDot($file) {
if ($file == '.' || $file == '..') {
return true;
}
return false;
}

function randomFiles($path, $numberToReturn = null) {
if (!is_dir($path)) {
return false;
}
$dir = opendir($path);
$files = array();
while($file = readdir($dir)) {
if(!isDot($file)) {
$files[] = $file;
}
}
shuffle($files);
if ($numberToReturn != null) {
$files = array_slice($files, 0, $numberToReturn);
}
return $files;
}

Test:


var_dump(randomFiles(dirname(__FILE__), 5));

JasonDFR
09-08-2009, 08:48 PM
Must have been working on these while the first reply was made.

OOP way. Just because it was fun to do.


class FileRandomizer {

protected $_dirPath = '';
protected $_numberToReturn = null;

public function __construct($dirPath, $numberToReturn = null)
{
if (!is_dir($dirPath)) {
throw new Exception('Path is not valid');
}
if ($numberToReturn !== null && is_int($numberToReturn)) {
$this->_numberToReturn = $numberToReturn;
}

$this->_dirPath = $dirPath;
}

public function getRandomFiles()
{
$filesArray = $this->_getFilesArray();
$randFilesArray = $this->_randomize($filesArray);
if ($this->_numberToReturn != null) {
$randFilesArray = array_slice($randFilesArray, 0, $this->_numberToReturn);
}
return $randFilesArray;
}

public function setNumberToReturn($numberToReturn)
{
if (!is_int($numberToReturn)) {
throw new Exception('Number to return must be an integer');
}

$this->_numberToReturn = $numberToReturn;
}

protected function _getFilesArray()
{
$dir = opendir($this->_dirPath);
$files = array();
while($file = readdir($dir)) {
if(!$this->_isDot($file)) {
$files[] = $file;
}
}

return $files;
}

protected function _randomize($files)
{
shuffle($files);
return $files;
}

protected function _isDot($file)
{
if ($file == '.' || $file == '..') {
return true;
}
return false;
}

}

Use:


$fileRandomizer = new FileRandomizer(dirname(__FILE__));
$fileRandomizer->setNumberToReturn(5);
var_dump($fileRandomizer->getRandomFiles());

techietim
09-08-2009, 09:33 PM
<?php
$directory = './files/'; //include trailing slash

$files = glob($directory . '*');
$files = array_filter($files, 'is_file');

echo '<ul>';
foreach(array_rand($files, 5) as $key)
{
echo '<li><a href="' . $files[$key] . '">' . basename($files[$key]) . '</a></li>';
}
echo '</ul>';

JasonDFR
09-08-2009, 09:35 PM
<?php
$directory = './files/'; //include trailing slash

$files = glob($directory . '*');
$files = array_filter($files, 'is_file');

echo '<ul>';
foreach(array_rand($files, 5) as $key)
{
echo '<li><a href="' . $files[$key] . '">' . basename($files[$key]) . '</a></li>';
}
echo '</ul>';


nice...

JShor
09-08-2009, 10:25 PM
<?php
$directory = './files/'; //include trailing slash

$files = glob($directory . '*');
$files = array_filter($files, 'is_file');

echo '<ul>';
foreach(array_rand($files, 5) as $key)
{
echo '<li><a href="' . $files[$key] . '">' . basename($files[$key]) . '</a></li>';
}
echo '</ul>';


and look at me, doing things the overcomplicated way :P

Jeffreyv1987
09-08-2009, 11:28 PM
Thanks again guys i got it working.