random - Include php file randomly -
i have question php include. here goes existing code 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 works me content displaying randomly. but, better. because, using code, everytime have added new content content5.php
, content6.php
, on, have bother update code above make new content.php
appear.
is there solution can have, not bother code above everytime add new content.php
, new content.php
appears automatically when added? possible?
updated: new testing code(tested failed again part of 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 ] );
Comments
Post a Comment