我的随机化代码无法离线工作

2023-12-29

我是一个 php 菜鸟,我只是根据我在网上找到的其他一些脚本制作了一个小脚本。它从名为“Random”的文件夹中随机选取 3 张图像并显示它们。

当我在线运行脚本时它可以工作,但是当我尝试在 xampp 上离线运行它时,我收到此错误:

注意:未定义变量:C:\xampp\htdocs\sito\finaleasd2.php 第 69 行中的 random2

在此行将图像的名称作为变量指定。 我认为这是由于脚本在离线时无法正确获取文件夹名称,但我不确定:出了什么问题? :)

顺便说一句,这是脚本,第 69 行是while (!$random2 || $random2 == $random1){(我知道,这很混乱!:D)

感谢您的帮助和时间! :)

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

}

//assegna i nomi delle variabili ai file
$random1 = RandomFile("random");
while (!$random2 || $random2 == $random1) {
    $random2 = RandomFile("random");
}
while (!$random3 || $random3 == $random1 || $random3 == $random2) {
    $random3 = RandomFile("random");
}


//la parte dedicata alla creazione dei testi alternativi partendo da un file di testo

$quotesfile = "quotes.txt"; //Relative path to and the filename of the file that contains your quotes. 



$array = @file("$quotesfile");  
// Crea un array con le citazioni
$quote = rand(0, count($array)-1);


$titolo = array_rand($array, 3);



// la parte sotto crea un div con dentro due immagini statiche, i lati della panchina, e quattro caricate a caso. le immagini hanno
// come titoli le variabili estratte casualmente dall' array di nome array preso dal file di testo di prima
?>

如果你唯一的问题是undefined variable, 这很容易。

本地和远程服务器上的服务器设置不同,因此一个返回错误,另一个则不返回错误,但是当您询问时!$random2在它存在之前它会返回一个错误。

所以只需设置$random2 to false之前while loop.

同样适用于$random3.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我的随机化代码无法离线工作 的相关文章

随机推荐