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

2019-06-06 08:28发布

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?

标签: php random
1条回答
做自己的国王
2楼-- · 2019-06-06 08:54

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.

查看更多
登录 后发表回答