我是一个php noob,我刚刚从网上找到的一些其他脚本中制作了一个小脚本。它从一个名为"random"的文件夹中随机选取3张图像并进行显示。
当我在线运行脚本时,它是有效的,但当我尝试在examplep上离线运行它时,我会收到以下错误:
注意:第69行C:\examplep\htdocs\sito\finaleasd2.php中的未定义变量:random2
这是一行,在那里图像被赋予它们的名字作为一个变量。我认为这是由于脚本在脱机时没有正确获取文件夹名称,但我不确定:出了什么问题?:(
顺便说一句,这是脚本,第69行是而(!$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
存在之前询问它时,它将返回错误。
因此,只需在while
循环之前将$random2
设置为false
即可。
$random3
也是如此。