簡體   English   中英

隨機包含php文件

[英]Include php file randomly

我對php include有疑問。 這是我現有的代碼。

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

現在,由於內容隨機顯示,因此這段代碼對我來說非常有效。 但是,我想變得更好。 因為,通過使用此代碼,每次我都像添加了新的內容content5.phpcontent6.php等等,我費心去更新上面的代碼,使新content.php出現。

有什么解決方法,我每次添加新的content.php都不會打擾上面的代碼,新的content.php在添加時會自動出現? 可能嗎?

更新:新的測試代碼(由於部分頁面錯誤,測試再次失敗)

    <?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