Date based header image not working correctly

I’m trying to design a site where it has a new header image every day. If there is a file that corresponds to a certain day (special events, etc), load that instead. It’s loading the daily image just fine. I’m having problems with the special images.


<?php
$file = "header/" . date("m-d") . ".png";
if (file_exists($file)) {
header("location: $file");
} else {
$file = "header/" . date("d") . ".png";
header("location: $file");
}
?>

Any Idea why it’s not working? I have an image 08-21.png and it’s not loading it.

Why would you redirect the browser to the image file?
Wouldn’t just echoing <img src=‘$file’> be more appropriate?

Have you tried echoing $file to be sure the name the script is looking for is what you’ve actually named the file?

I set the background in the css, using css similar to this:

#header {background: url("header.php");}

I have tried, it just ignores the special header image and defaults to the image for the current day of the month.

Your code tries the current day of month image first. If it’s found, it redirects to that. So you’ll always get that image if it exists.

If the special image should take priority it must be in the if() branch, and the default image in the else(). Make sense?