简体   繁体   English

随机包含php文件

[英]Include php file randomly

I have a question about php include. 我对php include有疑问。 Here goes the existing code that i have. 这是我现有的代码。

<?php
srand();
$files = array("folder/content1.php", "folder/content2.php", "folder/content3.php", "folder/content4.php");
$rand = array_rand($files);
include ($files[$rand]);
?>

This code actually works very well for me now as the content displaying randomly. 现在,由于内容随机显示,因此这段代码对我来说非常有效。 But, i would like to get something better. 但是,我想变得更好。 Because, by using this code, everytime i have added a new content like content5.php , content6.php and so on, i have to bother to update the code above to make new content.php appear. 因为,通过使用此代码,每次我都像添加了新的内容content5.phpcontent6.php等等,我费心去更新上面的代码,使新content.php出现。

Is there any solution i can have, to not to bother the code above everytime i add a new content.php and the new content.php appears automatically when added? 有什么解决方法,我每次添加新的content.php都不会打扰上面的代码,新的content.php在添加时会自动出现? Is it possible? 可能吗?

Updated: New testing code(tested failed again with part of my page mising) 更新:新的测试代码(由于部分页面错误,测试再次失败)

    <?php
$sDirectory     = 'myfolder';
if( is_dir( $sDirectory ) ) 
{
    $rDir = opendir( $sDirectory );
    while( ( $sFile = readdir( $rDir ) ) !== FALSE ) 
    {
        if( ( $sFile == '.' ) || ( $sFile === '..' ) )
        {
            continue;
        }
        $aFiles[] = $sDirectory . '/' . $sFile;
    }
}
$sRandom = array_rand( $aFiles );
require_once( $aFiles[ $sRandom ] );
?>
$sDirectory     = 'includes';
if( is_dir( $sDirectory ) ) 
{
    $rDir = opendir( $sDirectory );
    while( ( $sFile = readdir( $rDir ) ) !== FALSE ) 
    {
        if( ( $sFile == '.' ) || ( $sFile === '..' ) )
        {
            continue;
        }
        $aFiles[] = $sDirectory . '/' . $sFile;
    }
}
$sRandom = array_rand( $aFiles );
require_once( $aFiles[ $sRandom ] );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM