1

ディレクトリから10個のランダムな画像を取得してページに表示するphpスクリプトがあります。rand_boats.php という名前のインクルードを介して、このスクリプトをカスタムのワードプレス テンプレート ページに含めています。無効なフォルダが指定されたというエラー メッセージが表示され続けます。rand_boats と boats の両方のディレクトリが同じディレクトリにあります。スクリプトは次のとおりです。

<?php function RandomFile($folder='', $extensions='.*'){
   // fix path:
    $folder = trim($folder);
    $folder = ($folder == '') ? './' : $folder;

    // check folder:
    if (!is_dir($folder)){ die('invalid folder given!'); }

    // create files array
    $files = array();

    // open directory
    if ($dir = @opendir($folder)){

        // go trough all files:
        while($file = readdir($dir)){

            if (!preg_match('/^\.+$/', $file) and 
                preg_match('/\.('.$extensions.')$/', $file)){

                // feed the array:
                $files[] = $file;                
            }            
        }        
        // close directory
        closedir($dir);    
    }
    else {
        die('Could not open the folder "'.$folder.'"');
    }

    if (count($files) == 0){
        die('No files where found :-(');
    }

    // seed random function:
    mt_srand((double)microtime()*1000000);

    // get an random index:
    $rand = mt_rand(0, count($files)-1);

    // check again:
    if (!isset($files[$rand])){
        die('Array index was not found! very strange!');
    }

    // return the random file:
    return $folder . "/" . $files[$rand];

}


$random1 = RandomFile("img");
while (!$random2 || $random2 == $random1) {
    $random2 = RandomFile("img");
}
while (!$random3 || $random3 == $random1 || $random3 == $random2) {
    $random3 = RandomFile("img");
}
while (!$random4 || $random4 == $random1 || $random4 == $random2 || $random4 == $random3) {
    $random4 = RandomFile("img");
}
while (!$random5 || $random5 == $random1 || $random5 == $random2 || $random5 == $random3 || $random5 == $random4) {
    $random5 = RandomFile("img");
}
while (!$random6 || $random6 == $random1 || $random6 == $random2 || $random6 == $random3 || $random6 == $random4 || $random6 == $random5) {
    $random6 = RandomFile("img");
}
while (!$random7 || $random7 == $random1 || $random7 == $random2 || $random7 == $random3 || $random7 == $random4 || $random7 == $random5 || $random7 == $random6) {
    $random7 = RandomFile("img");
}
while (!$random8 || $random8 == $random1 || $random8 == $random2 || $random8 == $random3 || $random8 == $random5 || $random8 == $random5 || $random8 == $random6 || $random8 == $random7) {
    $random8 = RandomFile("img");
}
while (!$random8 || $random9 == $random1 || $random9 == $random2 || $random9 == $random3 || $random9 == $random5 || $random9 == $random5 || $random9 == $random6 || $random9 == $random7 || $random9 == $random8) {
    $random9 = RandomFile("img");
}
while (!$random8 || $rand10 == $random1 || $random10 == $random2 || $random10 == $random3 || $random10 == $random5 || $random10 == $random5 || $random10 == $random6 || $random10 == $random7 || $random10 == $random87 || $random10 == $random9) {
    $random10 = RandomFile("img");
}
?>

<div id="random_images">
<ul>
    <li><img src="<?php bloginfo('template_directory'); ?>/<?php echo $random1; ?>" alt="image alt" /></li>
    <li><img src="<?php bloginfo('template_directory'); ?>/<?php echo $random2; ?>" alt="image alt" /></li>
    <li><img src="<?php bloginfo('template_directory'); ?>/<?php echo $random3; ?>" alt="image alt" /></li>
    <li><img src="<?php bloginfo('template_directory'); ?>/<?php echo $random4; ?>" alt="image alt" /></li>
    <li><img src="<?php bloginfo('template_directory'); ?>/<?php echo $random5; ?>" alt="image alt" /></li>
    <li><img src="<?php bloginfo('template_directory'); ?>/<?php echo $random6; ?>" alt="image alt" /></li>
    <li><img src="<?php bloginfo('template_directory'); ?>/<?php echo $random7; ?>" alt="image alt" /></li>
    <li><img src="<?php bloginfo('template_directory'); ?>/<?php echo $random8; ?>" alt="image alt" /></li>
    <li><img src="<?php bloginfo('template_directory'); ?>/<?php echo $random9; ?>" alt="image alt" /></li>
    <li><img src="<?php bloginfo('template_directory'); ?>/<?php echo $random10; ?>" alt="image alt" /></li>
</ul>
   </div>
4

1 に答える 1

0

ディレクトリかどうかを確認する直前に $folder の値を出力すると、何が得られますか?

于 2012-05-04T19:54:12.087 に答える