Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > How To Handle “Missing Files” When I Reorganize My Hosted Images?

How To Handle “Missing Files” When I Reorganize My Hosted Images?
Thread Tools
ghporter
Administrator
Join Date: Apr 2001
Location: San Antonio TX USA
Status: Offline
Reply With Quote
Mar 30, 2024, 11:00 AM
 
All of the images I post here and elsewhere are hosted on my personal website. Technically, they’re hosted on my hosting service, and aren’t actually part of a website, just in folders under my site.

I want to reorganize (or maybe “finally initially organize”) my image files. Many of them aren’t in any sort of logical directory structure beyond “PubPics/MacNN/xx.jpg” (if that) which let me post them, but made a mess of that particular folder. So when I rename, move, and organize my images, a lot of old posts are going to have missing images. And that’s really annoying for someone who finds an old thread through a search engine…

I would like to have at least a custom “missing image” file, a small JPEG that isn’t as irritating as a little box with an X in it. And if I can do that, the JPEG could conceivably have text that says “email me”, so I can know if the old posts are actually being visited at all.

I already have a custom 404 page on my site, and it correctly displays if I navigate to a nonexistent image file, but that won’t (I don’t think) do anything like I want.

Any pointers?

Glenn -----OTR/L, MOT, Tx
     
reader50
Administrator
Join Date: Jun 2000
Location: California
Status: Offline
Reply With Quote
Mar 30, 2024, 01:42 PM
 
Your 404 "page" would have to return an image instead of a page. Ideally, you could do two 404s - one for requests to the old macnn folder tree, and the general one that would remain a page.

I've never tried to set up something like that - suggest reading the Apache docs on handling 404s.

You could also log which images are requested through the 404 image, along with referrer. That way you could fix the most common 404s - sort the log entries by hits, go to the referrer (thread) and edit the post in question. With the updated image location.
     
ghporter  (op)
Administrator
Join Date: Apr 2001
Location: San Antonio TX USA
Status: Offline
Reply With Quote
Mar 30, 2024, 06:35 PM
 
It definitely sounds like the Apache docs are where I need to start. My web host uses Apache, so anything I need to set up I can test at home before I upload it.

Since all of the images I’m worried about are in my PubPics folder, it may be as simple as a second 404 page for that folder, one that serves an image instead of a rendered HTML page, probably done through PHP. I’m having fun finding out what I can do with PHP, so this sounds like a nice challenge in that arena.

Glenn -----OTR/L, MOT, Tx
     
ghporter  (op)
Administrator
Join Date: Apr 2001
Location: San Antonio TX USA
Status: Offline
Reply With Quote
Mar 30, 2024, 08:41 PM
 
I couldn’t find anything errors for specific folders in the Apache documentation. When one clicks on a “no picture” icon in one of my posts, it does produce my custom 404. But it still gives that “no picture” icon. But not here.

I changed the file name for the rogue ad I posted in that thread, and when I went to that post, there wasn’t anything…just the text. No ‘missing image”, nothing. So I’m confused.

I’m also wondering if I need to worry about this much anyway. Most of my images were posted in a forum I haven’t logged into since May, 2021. I don’t know if anything I posted there is still relevant. But I really do want to find out how to manage this.

Glenn -----OTR/L, MOT, Tx
     
reader50
Administrator
Join Date: Jun 2000
Location: California
Status: Offline
Reply With Quote
Mar 30, 2024, 11:49 PM
 
Your 404 is returning a no-pic icon ... within a page. When a forum expects a pic, it requests one. When your browser gets a page unexpectedly, it shows nothing. If the forum post had listed an iframe instead of a pic, your browser would expect a page and would have rendered it in place.

If you want a no-pic icon, you'll have to rig your 404 to return just the image file. Not within a blank page.

If Apache doesn't want to provide multiple 404 options, then point it to a php file. Where you process the failed URL and either return the standard 404 page, or something else. Depending on what the user had tried to fetch. You could trap it based on the */macnn/* URL, and/or recognizing an image filename extension.

Logic flowchart:
- Check for "*/MacNN/*" in the URL.
-- Yes -> return custom MacNN no-pic-found icon
- Check for "*/other forum/*" in the URL.
-- Yes -> return custom icon for other forum
- Check for .jpg .png .gif .porn .wp .ect on the end of the URL (strip any variable extensions first)
-- Yes -> return a generic icon for image-not-found
- Return normal 404 page with file-not-found icon
     
ghporter  (op)
Administrator
Join Date: Apr 2001
Location: San Antonio TX USA
Status: Offline
Reply With Quote
Apr 1, 2024, 02:19 PM
 
Thanks, that makes a lot of sense. Also, your logic flow description simplified the PHP for me. It can be a fairly simple decision tree, with options for returning whatever error responses I have prepared. And since I already have my basic page grabbing the requested URL, I'm farther ahead than I had expected to be.

Later... Since I already am using PHP's $_SERVER['REQUEST_URI'] function, which returns the path to the requested page/image, all I need to do is break that out for folder, extension, etc., and then act accordingly. MUCH easier than I'd thought!
( Last edited by ghporter; Apr 1, 2024 at 03:00 PM. )

Glenn -----OTR/L, MOT, Tx
     
reader50
Administrator
Join Date: Jun 2000
Location: California
Status: Offline
Reply With Quote
Apr 1, 2024, 09:46 PM
 
If you want to be really fancy, you could use the 404 traps for forum images ... to return the desired image. From it's new location.

However, be very careful with this. If a requested image isn't present, you'll crash your 404 script, and possibly create an endless server error loop - because you're already in the 404 error routine. This can happen even if you didn't change anything on your server. ie - a search index spyder looking for images, tries to load alternate image numbers, and requests one that does not exist.

So you should check to see if file_exists before claiming a call that would result in returning that image file. Actually, you should do that with even your usual file-not-found image. If one were moved / deleted / renamed, you risk creating an endless 404 loop. Rig things so if no image files are present, your script will ultimately return a text 404 message. Error-handling routines need to be bug-free, or you'll be pulling your hair out in no time.
     
ghporter  (op)
Administrator
Join Date: Apr 2001
Location: San Antonio TX USA
Status: Offline
Reply With Quote
Apr 2, 2024, 09:31 PM
 
I’ve gotten as far as using PHP to create a JPG image with my text - which includes the URL of the missing file. I haven’t yet done any timing to see, but this may be a lot faster than using an existing image file and putting text over it.

I’ve tested this approach on my home server, and some time tomorrow, if I have the chance, I’ll put the appropriate files on my public server. It turned out to be pretty simple to do, once I had an idea of what I needed to do. As usual, my largest obstacles were mismatched parens and brackets, or using the wrong access methods for a couple of structures.

I decided that, instead of “fixing” the missing file, I’d first see how big a deal moving files around was to folks visiting various forums where I’ve posted. Log files will tell me that; requests that generate a 404 error are (I think) in logs. I just need to find out how to look at the logs on my hosting service. And files that generate a lot of errors would be the ones I’d “fix” anyway, so I definitely need to collect data before I go beyond where I am now.

Glenn -----OTR/L, MOT, Tx
     
ghporter  (op)
Administrator
Join Date: Apr 2001
Location: San Antonio TX USA
Status: Offline
Reply With Quote
Apr 2, 2024, 10:01 PM
 
…And getting at my logs turns out to be pretty easy. I haven’t done more than scan them so far, but today’s log shows a whole lot of 404s, 301s, and even some 401s (unauthorized) and 403s (forbidden). It looks like most of the hits are bots, but there was a period of time when an IP from India was trying every trick in the book to get access to upload, download, or modify files. My little site gets attention from around the world - but that’s not necessarily a good thing…

Glenn -----OTR/L, MOT, Tx
     
reader50
Administrator
Join Date: Jun 2000
Location: California
Status: Offline
Reply With Quote
Apr 2, 2024, 10:36 PM
 
So long as your server is up-to-date on security, don't worry about it. I once put an old security-cam DVR online. Turned on the built-in webserver, and went to bed. The next morning, nothing would load. Bots had found the new login page, and hammered it into crashing. They definitely didn't get in because no one could get in until I rebooted the box.

The bots & script kiddies are quick today.
     
ghporter  (op)
Administrator
Join Date: Apr 2001
Location: San Antonio TX USA
Status: Offline
Reply With Quote
Apr 6, 2024, 05:14 PM
 
The logs specifically showed failed attempts, so I’m comfortable that the host and the default security setup are effective.

I’ve pretty much finished my overbuilt 404 system. It checks the missing URI for whether its extension indicates a typical image file. If it is, it creates an image on the fly (I’ve discovered PHP’s GD library) with the URI of the missing image file. If not, it returns a PHP/HTML page with the missing URI.

That’s not the overbuilt part. The image routine figures out if the text of the URI will fit in the 400X600 image, and breaks it up so it will. Which sounded straightforward until I started working on it. I learned a lot - particularly to use a monospaced font for the URI…

I’m about to test it on a “live” server, which may reveal errors I didn’t find before, but that’s what testing is for, right?

Glenn -----OTR/L, MOT, Tx
     
ghporter  (op)
Administrator
Join Date: Apr 2001
Location: San Antonio TX USA
Status: Offline
Reply With Quote
Apr 6, 2024, 06:09 PM
 
Let’s try this:
This is a valid image:


This is not - I changed the extension…


The replacement image is 400X600, so it doesn’t get resized, but it’s quite readable. In theory I could have created a PNG which would have been clearer/cleaner, but with this size, the display would have to be downsized a lot before that would be an issue.

I’m pretty happy with the results.

Glenn -----OTR/L, MOT, Tx
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 05:39 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,