0

I would like to use a <?php include ... command to choose one random php file from one folder.

So this is the basic idea:

<?php include 'random file: 1.php, 2.php, 3.php or 4.php';?>

I have already read articles like this or this, but they don't answer my question clearly. What is the easiest way to do that?

Anna_B
  • 820
  • 1
  • 4
  • 23
  • create an array of files that are in that folder, count the entries in the array, and assign a random number number out of max entries in that array, and include that. – Dorvalla Sep 01 '22 at 22:27
  • @Dorvalla Makes sense. I think my problem is not the logic, but the coding. I barely use PHP, and don't know how to program it. It would be easier for me to do it with JavaScript. – Anna_B Sep 01 '22 at 22:41
  • Does this answer your question? [Include php file randomly](https://stackoverflow.com/questions/29179335/include-php-file-randomly) – Don't Panic Sep 02 '22 at 08:33
  • Or https://stackoverflow.com/questions/4478783/select-random-file-from-directory, https://stackoverflow.com/questions/29428584/jquery-php-how-to-get-random-file-in-folder, https://stackoverflow.com/questions/52874505/get-random-file-from-dir, https://stackoverflow.com/questions/56450514/how-do-call-a-random-php-file-from-another-directory ... – Don't Panic Sep 02 '22 at 08:35
  • @Don'tPanic - Problem with all your linked questions is: They either require manually/hard-coding files to an array or include directories in the list (they don't filter for just files), which is what OP implied in the question. – Jim Sep 02 '22 at 14:01
  • @Jim No, they do not. The *question* in the first dup linked shows how to do exactly what OP is asking, using a list of files very like `1.php, 2.php, 3.php or 4.php`. If that isn't actually what OP is after, the accepted answer to that question shows a way to list files in a directory, of course excluding directories (seriously!?), and choose a random one. The other answers go on to show many variations of the same, in case either of those solutions aren't quite right. – Don't Panic Sep 02 '22 at 14:17
  • That question only removes . and .. A named directory would still be included. – Jim Sep 02 '22 at 14:20
  • You're right. How about (taken from the other dups I linked) https://stackoverflow.com/a/4478788/6089612, https://stackoverflow.com/a/8287423/6089612, https://stackoverflow.com/a/29428690/6089612, https://stackoverflow.com/a/56451077/6089612, ...? – Don't Panic Sep 02 '22 at 14:44
  • Oh, hello! What would your recommendation to do it? I am actually not sure yet what way I should take. – Anna_B Sep 03 '22 at 23:19
  • @Anna_B How you do it is entirely up to you and your requirements. If you're not sure, read through the answers and see which makes the most sense to you, or seems to suit your code and circumstances the best ... or even which one is just easiest! I included the list to show there are many solutions here already; in general with a bit of searching you're likely to find answers to almost any question you have, especially with languages like PHP that have been around a while. – Don't Panic Sep 05 '22 at 07:18

2 Answers2

0
// Directory to use
$directory = '.';

// Filter out directories, we only want files.
$files = array_filter(scandir($directory), fn($f) => is_file($f));

// Pick a random file
$randFile = $directory . '/' . $files[array_rand($files)];

// Include it
include $randFile;
Jim
  • 3,210
  • 2
  • 17
  • 23
0

to include random files from another directory

  $path = __DIR__."/folder";
 
  foreach(new DirectoryIterator($path) as $file){
      if($file->isFile()){
        $arr[] = $file->getFilename();
      }
  }
  $randFile = $path."/".$arr[array_rand($arr)];

  include $randFile;
Flaaim
  • 34
  • 1
  • 4