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

  • Home
  • SEARCH
  • 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 746723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:07:24+00:00 2026-05-14T14:07:24+00:00

I have a function that will be passed in a link. The link is

  • 0

I have a function that will be passed in a link. The link is to a remote image. I thought I could just use the extension of the file in the URL to determine the type of image but some URLs won’t have extensions in the URL. They probably just push headers to the browser and therefore I do not have an extension to parse from the URL.

How can I test if the URL has an extension and if not then read the headers to determine the file type?

Am I over complicating things here? Is there an easier way to do this? I am making use of Codeigniter maybe there is something already built in to do this?

All I really want to do is download an
image from a URL with the correct
extension.

This is what I have so far.

function get_image($image_link){

    $remoteFile = $image_link;

    $ext = ''; //some URLs might not have an extension

    $file = fopen($remoteFile, "r");


    if (!$file) {

        return false;

    }else{

        $line = '';

        while (!feof ($file)) {

            $line .= fgets ($file, 4096);

        }

        $file_name = time().$ext;

        file_put_contents($file_name,  $line);

    }

    fclose($file);

}

Thanks all for any help

  • 1 1 Answer
  • 1 View
  • 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-14T14:07:25+00:00Added an answer on May 14, 2026 at 2:07 pm

    You never want to switch on the file extension. For example, not all ASCII text files will have “.txt”, and then there’s always the fun “.jpg” versus “.jpeg”. Instead, switch on the Content-Type header that the web server responds with. (See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17).

    Some web servers will also respect the Accept header (see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1). So, if you know you only want PNG image files, then send Accept: image/png. If the server respects it, then its response will be much smaller than sending the whole unwanted file over the network connection. However, because not all web servers do this well (if at all), make sure you still switch on the Content-Type response header. It’s just a bandwidth saving thing.

    You could also send Accept: image/* to get any type of image.

    Cheers.

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

Sidebar

Ask A Question

Stats

  • Questions 453k
  • Answers 453k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer See this explanation from the Java tutorial: Interestingly, the TreeModel… May 15, 2026 at 9:33 pm
  • Editorial Team
    Editorial Team added an answer This solution is for a ListBox, but it could be… May 15, 2026 at 9:33 pm
  • Editorial Team
    Editorial Team added an answer Something else is going on here. I use popToRootViewControllerAnimated in… May 15, 2026 at 9:33 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.