Avatar of J N
J N
 asked on

detect if image exists

Hi,

i am working with some external data from twitter and occasionally it displays a link to an image in one of its array values.

If i try and use the img tag and display the image i get an empty field.

i am curious if it is possible to test if the link is an image file. i have tried using file_get_contents however the page loads very slowly but it does work.

i am wondering if i could use is_file or file_exists with a url such as

https://pbs.twimg.com/profile_banners/334036628/1395969403

to determine if the file exists as it is not always the case

thanks in advance
PHP

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
Julian Hansen

There are some comments in the PHP man page for file_exists that might be of use

http://www.php.net/manual/en/function.file-exists.php#75064
$file = 'http://www.domain.com/somefile.jpg';
$file_headers = @get_headers($file);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
    $exists = false;
}
else {
    $exists = true;
}

Open in new window

And http://www.php.net/manual/en/function.file-exists.php#74469
<?php
function url_exists($url) {
    if (!$fp = curl_init($url)) return false;
    return true;
}
?>

Open in new window

J N

ASKER
Thanks

Which one would be faster?
Ray Paseur

PHP is_file() and file_exists() are designed to work on your own server, and even though the man pages say that "some protocols are supported" you're dependent on a foreign server for the support.  PHP file_get_contents() may be unreliable because (1) some servers will block it and (2) if it does not get a response, it hangs your script until timeout, then you get a fatal error.

Your best bet is probably cURL.

I'll see if I can give you a script that will try to detect a file on a foreign server and will time the attempt.  Then you can experiment with it to choose a way that is (hopefully) both fast and reliable.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
SOLUTION
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Ray Paseur

Key to the question is probably "discernible difference."  The time required to check the URLs is something that is interesting, but it is not under the author's control.  It is accomplished via the response from a remote server.  For all we know that server could respond quickly and consistently, or be erratic and sometimes very slow, or it could cache the responses, making all 2+ iterations of the test dilutive of the expected results that would arise in a real-world scenario.  This appears to be the case when I tested your script.  The first access with get_headers() took almost five times longer than subsequent accesses.  The actual elapsed times are measured in milliseconds, not fractions of milliseconds.

The Stopwatch class is just my easy way of getting a measurement -- it's not really an integral part of the solution - just a sidebar note to answer the question of "which is faster."  Given the test case, cURL seems faster.  But it's not really under our Author's control.
J N

ASKER
WOW!! thanks for the input!

Im not sure where i should take this. I am using it to access photos from twitter using the abraham library. Unfortunately, sometimes the information obtained from the library classes do not contain the full data and i need to attach info. the reason why i want to test to see if there is something there. I believe twitter has pretty stable servers.

Knowing that information which method should i select.

thanks
guys
J N

ASKER
Additionally, i am using the function to test about 200 photos
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Ray Paseur

I believe twitter has pretty stable servers.
Ha!  Twitter crashes and hangs all the time.  Let's do the arithmetic.

200 photos * 95 milliseconds (for not-found, less for found) = 19 seconds.  If you use cURL to read the image files, you can cache them on your own server and be done with it in less than a minute.
J N

ASKER
Hi,

I already did the math to get 19 seconds which is FAR better than what i have right now. However, im curious how that compared to the other methods compared above.
Ray Paseur

Try them and see!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
J N

ASKER
THANKS GUYS BIG HELP!!!
Julian Hansen

In my opinion it makes no difference - I would go with the get_headers code because it is less lines of code - I prefer less code - but there is nothing wrong with the cUrl solution.

If you can't decide - toss a coin.