Published in Web Development on Thursday, August 12th, 2004
Putting a custom 404 script to work behind the scenes to help find broken images on your site.
While monkeying around with an error document for a clients site, I accidentally discovered a neat solution to a problem we were having. While rather specific to the situation, I thought I'd post it up here as maybe someone could extend it in other directions, or maybe there are other ways to solve the issue we were having. Please, feel free to brainstorm or share ideas in the comments...
The client in question has roughly 600 images that are used on various pages within their website. They have in house staff that prep all of the images, thumbnails and larger versions. Image names are entered into their in-house database app, and the db and image files are uploaded/updated nightly.
As should be expected in such situations, sometimes either the thumbnail or large versions are mistakenly not created, or files are mis-named between the actual image and the value that they record in the database. This leads to problems in the form of missing images on their website.
The little trick involved here is quite simple, and relies on the fact that the missing image results in a 404 - not found error. By using a custom error document (in our case php driven - for more on error docs, perhaps start with The Great Custom 404 Page Adventure) and checking the
$_SERVER['REQUEST_URI'], we are able to check if the request was from the image folder, and if so record the name of the missing image in a database.
The kicker of this is that a list of broken images is built up and stored in the db as users browse the site. We pass on the list to the client and they clean up the mess. Quick and effective.
I started freelancing by diving in head first and getting on with it. Many years and a lot of experience later I was still able to take away some gems from this book, and there are plenty I wish I had thought of beforehand. If you are new to freelancing and have a lot of questions (or maybe don't know what questions to ask!) do yourself a favor and at least check out the sample chapters.
Like the other books listed here, this provides a great reference for the PHP developer looking to have the right answers from the right people at their fingertips. I tend to pull this off the shelf when I need to delve into new territory and usually find a workable solution to keep development moving. This only needs to happen once and you recoup the price of the book in time saved from having to develop the solution or find the right pattern for getting the job done..