OK, best I can figure is it was the permissions set on the original images.txt file and/or the fact that you created it - I could have been clearer about that. The server needs read write privileges on that file. And it cannot exist until the script creates it. If you know what you're doing or have some idea, you can edit it later for testing purposes, but never allow it to have only one line in it, and never remove the last line (the numeric date line). As long as you don't set permissions on it (just let the server decide as it creates and maintains it), you can always just delete it if you run into problems or want to change the the images in the flags folder. You can change them and not worry as long as they still have the same names. But if you add or remove images or change their names, you should delete the images.txt file and it will start over on its own. You should be able to use this new code without deleting your existing images.txt file. But you should be able to delete it at any time for testing purposes/whatever. Now, I've tested this as best as I think I can here. And you will have to edit the $links array (near the end - probably just use the same one you created for the other code), as I have no idea of the names of your image files or the pages associated with them. Otherwise, this should be a drop in replacement for what you just posted that's already working (save the the working version as a backup, just in case):
PHP Code:
<!DOCTYPE html>
<html>
<head>
<title>Random Image Each Midnight</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<a href="<?php
// MidnightRambler PHP Script (c)2018 John Davenport Scheuer
// as first seen in http://www.dynamicdrive.com/forums/
// username: jscheuer1 - This Notice Must Remain for Legal Use
function midnightrambler(){ // requires PHP 5 or greater
$path = "flags/"; // set path to images, include trailing slash, must be on the server AND relative to the current page
$imgfile = "flags/images.txt"; // set path and filename to a non-existing file to hold image names and current date, DO NOT CREATE THIS FILE!
$eol = PHP_EOL; // end of line reference
$date = date("zY", time()); // the current date as day number in the year, followed by the 4 digit year number
if(file_exists($imgfile)){$datedims = file($imgfile, FILE_IGNORE_NEW_LINES);}
else{$datedims = array();}
$c = count($datedims);
if ($c < 2 or ($c < 3 and $date !== $datedims[$c - 1])){ // it's the first time, file was edited improperly/deleted, or we're beyond time and only one image is left
$imar = $imar = glob("$path*.[Pp][Nn][Gg]"); // grab images as array (include/exclude image patterns if/as desired)
if($c > 1){ // if this isn't the first time and the $imgfile hasn't been deleted
if(($key = array_search($datedims[0], $imar)) !== false){ // and the previous image is still in the folder
array_splice($imar, $key, 1); // do not consider it in the next iteration
}
}
shuffle($imar); // random order for images (once shuffled will continue to be random until used up)
$imar[] = $date; // add the current date to the array for checking later
file_put_contents($imgfile, implode($eol, $imar), LOCK_EX); // save for future use
$datedims = $imar; // use current $imar
}
else if ($date !== $datedims[$c - 1]){ // there's images left but we're past time
array_shift($datedims); // get rid of the first image listing
array_pop($datedims); // remove old outdated date
$datedims[] = $date; // add the current date to the array for checking later
file_put_contents($imgfile, implode($eol, $datedims), LOCK_EX); // save for future use
}
$im = $datedims[0];
$links = array("dragonfly-2257281_1280.jpg" => "http://www.google.com/", // change these to reflect your filenames and links
"bird-2252045_1280.jpg" => "http://www.dynamicdrive.com/");
echo $links[basename($im)] . '"><img src="' . $im . '" alt="Flag'; // write result (first item now in the $datedims array)
}
midnightrambler();
?>
"></a>
</body>
</html>
Notes: There are changes both within and without the PHP code block, so just copy and use this as is, except of course for entering your own $links array. I changed the GLOB expression to eliminate GLOB_BRACE, as it's not really used in your setup, and added a GLOB convention that makes its testing of the extension case insenitive, just in case that ever becomes an issue. I've changed some comments to make some things clearer, hopefully and done some of the things I mentioned earlier - encapsulation to protect it from other code and to protect other code from it, and (because it was now safe to do so) used the $c variable to conserve on the count($datedims)
expression. I've also added a credit, which is not mandatory, but since no one but you or other web masters will see it, all it will do is remind one where to come for help later. BTW, though it might not matter, the permissions on my images.txt (automatically generated by the server) are 644. I think part (possibly all) of the problem may have been my logic in analyzing the length of the $datedims array. I've tried to correct that in the current version. However, clearly, a text file never needs execute permission for any of the three possible entities, so that may have entered into it. Still, I think the error was more likely mine in not correctly bringing logic to bear on the length of the $datedims array. It would work in ideal conditions, but not when mistakes entered in. That's tightened up now I think. Perhaps other things can be done, not sure at the moment.
Any problems or questions, just let me know.
Bookmarks