Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 523257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:26:20+00:00 2026-05-13T08:26:20+00:00

I’m looping through an array of URL strings of images hosted at an external

  • 0

I’m looping through an array of URL strings of images hosted at an external site.

It looks something like this:

def get_image_urls
  image_url_array.each do |image_url|
    puts image_tag image_url
  end
end

Which will return the URLs of images hosted on the external site. The problem is, some of these images might be broken (404). So for example:

get_image_urls
# These would return image_tags, but for brevity...
=> "http://someothersite.com/images/1.jpg"
   "http://someothersite.com/images/2.jpg"
   "http://someothersite.com/images/3.jpg" # <-- (Broken: 404)
   "http://someothersite.com/images/4.jpg"
   "http://someothersite.com/images/5.jpg" # <-- (Broken: 404)

What I’m looking to do is replace the URL strings of the broken images to a “missing” image hosted on my own site. So using the example above, with 3.jpg and 5.jpg being broken, I want to have returned something like this:

get_image_urls
# These would return image_tags, but for brevity...
=> "http://someothersite.com/images/1.jpg"
   "http://someothersite.com/images/2.jpg"
   "http://mysite.com/images/missing.png"
   "http://someothersite.com/images/4.jpg"
   "http://mysite.com/images/missing.png"

Is there a simple way to solve this problem? Thanks so much in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:26:20+00:00Added an answer on May 13, 2026 at 8:26 am

    Can’t you do a simple request for the image and check if its a 404? Its not perfect but would catch the bulk. Depends how often you are running it. If you don’t have access to the files on the server directly to check then a HTTP request is the only way. For speed you could do a header only request. Will need a code example, let me dig you one out…

    Depends on what your server will return, if you can get the headers and you just get a standard 404 page then you can check the content-length to ensure its not big enough for an image, that sounds a bit hacky but would work (theres a better way afterwards). Something like:

    (take and modified from http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html#M000682).

    response = nil
    Net::HTTP.start('www.example.com', 80) {|http|
      response = http.head('/myimage.html')
    }
    # Assume anything with a content-length greater than 1000 must be an image? 
    # You will need to tweek/tune this to your server and your definition of what a good image is
    p "Good image" unless response['content-length'] < 1000
    

    Alternatively you can (and should really to do it the right way) get the HTTP status message as thats the definitive way of the server telling you if the image is there or not. Only trouble is you might have to download the whole thing as I don’t know of a quick way of getting just the HTTP status without doing a whole request (see request method in the doc linked above for details).

    Hope that helps though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.