Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: {PHP} Display a different image at midnight every day

  1. #11
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,482
    Thanks
    79
    Thanked 3,446 Times in 3,407 Posts
    Blog Entries
    12

    Default

    Well, i would want to know which version of PHP you're using. But the main thing I see right away is:

    2. I created a images.txt file in which the image ulrs and dates will be written to. Gave it 755 permissions (Running Ubuntu 16.04 Linux, btw)
    That file should not have been created. The script will do it for you. Creating it might or might not be a problem. The only other thing that might be an issue there I can think of is:

    Code:
    $path = "../site5/cssimages/scaled/"; // set path to images, include trailing slash
    Make sure that points correctly to your image folder and that there are at least 5 images in there and that the trailing slash is used. Don't necessarily use the ../ prefix if that's not relevant on your system. It was on mine, that's the only reason it's there.

    Further, the url you're seeing is the date convention I created (day 105, in the year 2018), so likely having created the file is the problem and/or the image folder is not correctly referenced. Delete your images.txt file, make sure the $path var is set correctly for your system, and try viewing the random.php page again. The permissions you set might be persisting and still be an issue. Hopefully, probably, not. As that file only needs to be accessed by the server.

    If you want more help, please post your current code, your random.php file.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  2. #12
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,482
    Thanks
    79
    Thanked 3,446 Times in 3,407 Posts
    Blog Entries
    12

    Default

    Please also read my previous message. But here's more information on the $path var. It must be relative to the page that the script is functioning on and it must point to a folder on the server. It cannot be an absolute or a network path. So if your images are in a folder named images that is a child folder of where the page is, it should be:

    Code:
    $path = "images/";
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #13
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,482
    Thanks
    79
    Thanked 3,446 Times in 3,407 Posts
    Blog Entries
    12

    Default

    Oh, and also (please still read my previous two posts) file_put_contents (used by this code) requires PHP 5 or later. If you don't have that, I can work out something, already have a trial version for those who do not have file_put_contents.
    Last edited by jscheuer1; 04-18-2018 at 06:57 AM. Reason: update
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #14
    Join Date
    Jun 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Here is my current code John as of 4-18-2018:

    Hey John,

    On a hunch, I went to try the script...When I first ran the test (just a few seconds ago) the script was complaining about the images.txt file...Then I created that file in the flags folder with the images, and refreshed and the code works, it's displaying one flag! The only thing is I would love to each image to have a different link (url)...I guess I just had to put the images.txt file in the same folder as the images...Unless the path to the images.txt was incorrect, but in any case the code now works (coincidently)...

    I have advanced my clock by setting the time in the taskbar to manual then using:
    Code:
    date +%T -s "23:59:50"
    and waiting 10 seconds then hitting refresh, and wa-la, we have success!

    Here's the code:

    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>
    <img src="
    <?php
    $path 
    "flags/"// set path to images, include trailing slash
    $imgfile "flags/images.txt"// set path and filename to file to hold image names and current date, should not initially exist
    $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($imgfileFILE_IGNORE_NEW_LINES);}
    else{
    $datedims = array();}
    if (
    count($datedims) < or (count($datedims) < and $date !== $datedims[count($datedims) - 1])){ // it's the first time or we're beyond time and only one image is left
        
    $imar glob("$path{*.png}"GLOB_BRACE); // grab images as array (include/exclude image patterns if/as desired)
        
    if(count($datedims)){ // if this isn't the first time and the $imgfile hasn't been deleted
            
    if(($key array_search($datedims[0], $imar)) !== false){ // and the current image is still in the folder
                
    unset($imar[$key]); // 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($imgfileimplode($eol$imar), LOCK_EX); // save for future use
        
    $datedims $imar// use current $imar
    }
    else if (
    $date !== $datedims[count($datedims) - 1]){ // there're 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($imgfileimplode($eol$datedims), LOCK_EX); // save for future use
    }
    echo 
    $datedims[0]; // write result (first item now in the $datedims array)
    ?>
    ">
    </body>
    </html>
    Brian

  5. #15
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,482
    Thanks
    79
    Thanked 3,446 Times in 3,407 Posts
    Blog Entries
    12

    Default

    Great! So it's working. I was very excited too when I saw it working as expected.

    OK, just finishing up dinner here, may be busy for a bit. But I already have a good idea how to adapt this to utilize the associative array you must have created for the other thread that relates to this. First I will want to review the code you're using to see anything additional it can tell me, second I have updated code that's more foolproof/compatible with other code, but that's not necessary if what you have works, finally I'll present how the associative array and links can be worked in with both what you have, and the more recent code I've been using (incidentally it's already working fine here and on my live server as well).
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #16
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,482
    Thanks
    79
    Thanked 3,446 Times in 3,407 Posts
    Blog Entries
    12

    Default

    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($imgfileFILE_IGNORE_NEW_LINES);}
        else{
    $datedims = array();}
        
    $c count($datedims);
        if (
    $c or ($c 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$key1); // 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($imgfileimplode($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($imgfileimplode($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.
    Last edited by jscheuer1; 04-19-2018 at 09:26 PM. Reason: English Usage, and add info, later minor code improvement
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Replies: 0
    Last Post: 06-18-2013, 03:57 AM
  2. Display a default enlarged image using CSS Image Gallery
    By KCgame in forum Dynamic Drive scripts help
    Replies: 4
    Last Post: 08-14-2009, 06:54 AM
  3. Replies: 2
    Last Post: 02-08-2009, 08:42 PM
  4. display a text under each image in OO_CMotion Image Gallery
    By nuwaninfo in forum Dynamic Drive scripts help
    Replies: 12
    Last Post: 12-17-2008, 10:41 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •