Results 1 to 8 of 8

Thread: Directory Display.

  1. #1
    Join Date
    Aug 2009
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Directory Display.

    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 Code:
    <?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..

  2. #2
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    You can put the files into an array, then randomize it. Then, use array_slice() to limit the results.

    PHP Code:
    <?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($arr05);

      foreach(
    $args as $a) {

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

      }
    ?>
    HTH
    - Josh

  3. #3
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    PHP Code:
    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($files0$numberToReturn);
        }
        return 
    $files;

    Test:

    PHP Code:
    var_dump(randomFiles(dirname(__FILE__), 5)); 

  4. #4
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Must have been working on these while the first reply was made.

    OOP way. Just because it was fun to do.

    PHP Code:
    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($randFilesArray0$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:

    PHP Code:
    $fileRandomizer = new FileRandomizer(dirname(__FILE__));
    $fileRandomizer->setNumberToReturn(5);
    var_dump($fileRandomizer->getRandomFiles()); 
    Last edited by JasonDFR; 09-08-2009 at 09:11 PM.

  5. #5
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    PHP Code:
    <?php
    $directory 
    './files/'//include trailing slash

    $files glob($directory '*');
    $files array_filter($files'is_file');

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

  6. #6
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    Quote Originally Posted by techietim View Post
    PHP Code:
    <?php
    $directory 
    './files/'//include trailing slash

    $files glob($directory '*');
    $files array_filter($files'is_file');

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

  7. #7
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Quote Originally Posted by techietim View Post
    PHP Code:
    <?php
    $directory 
    './files/'//include trailing slash

    $files glob($directory '*');
    $files array_filter($files'is_file');

    echo 
    '<ul>';
    foreach(
    array_rand($files5) 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
    - Josh

  8. #8
    Join Date
    Aug 2009
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Thanks again guys i got it working.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •