Shortcut to the main page: Alt + Shift + upper 2(@)
Linkedin FB e-mail Google Plus Twitter

Search in this website

 
 
Language versions are not identical,
so visit all versions,
which you can understand.

PHP – Sample of random pictures from directory at the server
Random files with requested extensions; entered count or all,

 

Made
2013-06-17

 

PHP function for random selection of entered number of files (or all ones, if there is not enough of them; or all ones, if such request is entered)
with requested extensions from entered directory at server (webhosting).

Typically for selection and insertion of needed number of random pictures, e.g. with partners of project, sponsors, references to another projects, examples, or illustration and decorative graphics.

//Martin Adámek, www.adamek.cz
/////////////////////////////////////
//$extensions              List of allowed extensions (per 3 or 4 characters)   
//$path                    Path to the directory to exploration at the server (without slash at the end) 
//$how_much                Number of names of files to result (0 == all) 
///////////////////////////////////// 

  //Finding of names of requested number of random files with requested extensions in requested directory
  function return_random_filenames_from_directory($extensions = array('jpg','jpeg','png'), $path = '', $how_much = 1) // V definici funkce uvedeny výchozí hodnoty vstupních proměnných, které se implicitně použijí, když dané proměnné nebudou při volání funkce explicitně zadány 
    { //Initialization
      $how_much = intval($how_much);      //To treat texts, decimal numbers and another unpleasant values //After error or with array at the input returns 0 or 1
      if ($how_much < 0) $how_much = 0;   //To treat negative input 
      $files = array();                   //For found suiting filenames
      $which_ones = array();              //For keys referring to selected names of files
      $result = array();                  //For sample to return (selected names of files)

      //Loading of names of suiting files
      $directory = opendir($path); 
      while ($file = readdir($directory)) 
        if ( in_array( substr($file, strlen($file)-3, 3), $extensions) or in_array( substr($file, strlen($file)-4, 4), $extensions) ) // Note: For list of files with all extensions (any extension) without this restriction is necessary to filter out directory and upper directory: $file!='.' and $file!='..'  
          $files[] = $file;
      closedir($directory);

      //Selection of values to return
      if (/*count($files) <= 1 or */ $how_much == 0 or count($files) <= $how_much)     // Necessary to return all found filenames
                                                                                       //  Platné podmínky: Chce všechny; nebo se jich našlo méně než je požadováno
                                                                                       //  Zakomentovaná podmínka: Našel se žádný nebo jeden (netřeba samostatně řešit, řeší to další dvě podmínky, když nahoře zajišťujeme, že $how_much>=0 a je celé číslo)      
          $result = $files;                                                            // We are returning all ones  
      
      else                                                                             // Necessary to select just requested count
        { $which_ones = array_rand($files,$how_much);        
          if ($how_much == 1)                                                          // array_rand returns directly key
            $result[] = $files[$which_ones];                                           // …but this function will return it as first value of array, to it be possible to process results always the same way
          else                                                                         // array_rand returns array of keys
            foreach ($which_ones as $ktery) $result[] = $files[$ktery];
        }                                                                                      
      return $result;         //Array with filenames with requested extensions in requested directory      
    }


  //Insertion of images with logotypes of partners of project
  function insert_partners_logotypes()
    {     
      $files=return_random_filenames_from_directory(array('jpg','jpeg','png','gif'), '/img/logotypes', 2);            
      echo '<div id="project_partners">';
        foreach ($files as $file) echo '<img src="img/logotypes'.$file.'" alt="Partner projektu" title="Partner projektu" />'; // I když v tomto případě by se do altu správně měl psát přímo konkrétní název firmy (text viditelný na obrázku), což se tu neděje //V případě čistě dekorativního nebo jinak obsahově opravdu bezvýznamného obrázku nechte alt prázdný
      echo '</div>';      
    }

Although function array_rand() returns various results during various running (if it is possible to choose from more possibilities and isn't necessary to return all items),
but the items in result are always in the same mutual order of chosen items.
It is possible to use function shuffle() for randomizing of items' order.

Jump up to: Navigation menu
(keyboard shortcut Alt + Shift + „5”)

Are you interested by this page?

  • Add to bookmarks (Ctrl+D)
  • Share link (CB radio)Jump up to:
  • Print (Ctrl+P)
  • Cite according to ISO 690

    This page

    ADÁMEK, Martin. PHP – Sample of random pictures from directory at the server: Random files with requested extensions; entered count or all,. Martin Adámek [online]. Náchod / Meziměstí (Czech Republic) [cited 2024-07-27]. Available from: https://www.adamek.cz/en/sw/php/random-sample-of-images

    Whole site

    ADÁMEK, Martin. Martin Adámek [online]. Náchod / Meziměstí (Czech Republic) [cited 2024-07-27]. Available from: https://www.adamek.cz/en

 

 
 

National Cultural Heritage

WebArchiv – This website is being archived by the National Library of the Czech Republic This website is regularly archived by the National Library of the Czech Republic for its cultural, educational, scientific, research or other values with the aim of documenting an authentic sample of the Czech web. It belongs to a collection of Czech websites to be preserved by the National Library and made available for posterity. Its record is included in the Czech National Bibliography and the National Library catalogue.  

 

 
 

For a distraction

- Why „dark” is spelled with „K”, not with „C”?
- Because you can not C in the dark.

 

For a muse

The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts.
[Bertrand Russell]