Load in random .html file with PHP? [closed]

2019-06-06 08:20发布

问题:

I have a folder which contains several html files: 1.html, 2.html, 3.html, etc., etc. in sequential order.

I would like for PHP to randomly load in these files into a PHP webpage that I have. How can I go about doing this?

Also -- is PHP the most efficient way to do this? Would jQuery be better?

回答1:

jquery could do it, but you'd have to send a list of the available files to the client beforehand, so it has a list to choose from. This would be required if you can't guaranteed there'll never be "holes" in the files, e.g. 1,2,4,5 (hey, where's 3?).

PHP can deal with the raw filesystem, and can always get the list of files, e.g.

<?php
$files = glob('*.html');
$random_file = $files[array_rand($files)];
include($random_file);

This will handle any .html file, regardless of holes in the numbering sequence, or even if they're numbered at all.



标签: php random