I want to check an image hosted on another site/server if that exists or not at the time of code running. For this purpose I am using JavaScript that is working fine with my desire that I am sharing below. But some browsers doesn't support JavaScript or some users are using blocking extension to block JavaScript so at that time my code does run.
<script type='text/javascript'>
var url = '.jpg';
var img = new Image();
img.src = url;
img.onload = function()
{
alert('Image Is Available')
}
img.onerror = function()
{
alert('Image Is Not Available')
}
</script>
So now I want to do the same as in above code but by pure PHP. I don't want to use fsockopen
, file_get_contents
, curl
, get_headers
option to hit and check the server. I only want to hit image and check that like above. For this purpose I used the below code but its not working. Its showing Image Is Not Available
even I am able to open image directly in browser. I found this code over internet.
<?php
$url = '.jpg';
if (@GetImageSize($url)) {
echo "Image Is Available";
} else {
echo "Image Is Not Available";
}
?>
So what the error is in this code? Can you mention and solve...???
Update: Please don't mention about image load time or size as I am hitting favicon.ico
or other small size images on my target.
Important Problem: I check the above PHP code on another target and its working perfectly. But where I want to check the image is not working. Mine target didn't contain domain, Its an IP address like hhtp://XX.XX.XX.XX:XXXX/example.jpg
. So the code is correct but my target is not replying to any external codes hit. Its displaying the image when I open the link directly in the browser. SO my this question contain answer also. I will ask another question about my new problem.
I want to check an image hosted on another site/server if that exists or not at the time of code running. For this purpose I am using JavaScript that is working fine with my desire that I am sharing below. But some browsers doesn't support JavaScript or some users are using blocking extension to block JavaScript so at that time my code does run.
<script type='text/javascript'>
var url = 'http://www.example./example.jpg';
var img = new Image();
img.src = url;
img.onload = function()
{
alert('Image Is Available')
}
img.onerror = function()
{
alert('Image Is Not Available')
}
</script>
So now I want to do the same as in above code but by pure PHP. I don't want to use fsockopen
, file_get_contents
, curl
, get_headers
option to hit and check the server. I only want to hit image and check that like above. For this purpose I used the below code but its not working. Its showing Image Is Not Available
even I am able to open image directly in browser. I found this code over internet.
<?php
$url = 'http://www.example./example.jpg';
if (@GetImageSize($url)) {
echo "Image Is Available";
} else {
echo "Image Is Not Available";
}
?>
So what the error is in this code? Can you mention and solve...???
Update: Please don't mention about image load time or size as I am hitting favicon.ico
or other small size images on my target.
Important Problem: I check the above PHP code on another target and its working perfectly. But where I want to check the image is not working. Mine target didn't contain domain, Its an IP address like hhtp://XX.XX.XX.XX:XXXX/example.jpg
. So the code is correct but my target is not replying to any external codes hit. Its displaying the image when I open the link directly in the browser. SO my this question contain answer also. I will ask another question about my new problem.
-
Remove
@
beforegetImageSize()
. Also : php/manual/fr/function.file-exists.php – fdehanne Commented Sep 17, 2014 at 15:40 -
3
Why don't you want to use
file_get_contents
? If you use that, you could simply check the magic variablehttp_response_header
and it would contain an array of the headers, including an error code (302 or 404 or whatever). There's not going to be a perfect solution to this, because servers can respond to your request however they want, including not at all. The best you can do is look at the headers that the server returns. One useful header is theContent-Type: image/jpeg
. – Chris Middleton Commented Sep 17, 2014 at 15:40 - 1 Is allow_url_fopen On? (see phpinfo()) – Pascal Ockert Commented Sep 17, 2014 at 16:14
- @djidi Still not working after following your tip... – Muhammad Hassan Commented Sep 17, 2014 at 17:40
- @PascalOckert Yes, Its On... – Muhammad Hassan Commented Sep 17, 2014 at 17:41
4 Answers
Reset to default 3The below code you pasted should work for you:
<?php
$url = 'http://www.example./example.jpg';
if (@GetImageSize($url)) {
echo "Image Is Available";
} else {
echo "Image Is Not Available";
}
?>
Updated: Ensure outgoing requests to port 80 (or whatever port you use for the outgoing http request to fetch the image) is not blocked on the server.
Note: Thanks Pascal Ockert for pointing out GetImageSize doesn't require GD. That was what I thought all this time and a quick Google shows that many others too. This is a classic example of "pass knowledge to learn more".
Simplest of the answer is to use file_exists() function where param would be absolute file path, it returns true/false:
$isFileExists = file_exists($filePath);
<?php
$file = 'http://www.example./example.jpg';
$file_headers = @get_headers($file);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
$exists = false;
}
else {
$exists = true;
}
echo $exists;
?>
Try above code.
Update:
On second thought, I was wrong. Using a library like GD is the easiest and most reliable way to do this. I think you may just be using the function wrong (or not using GD?). The function getimagesize
returns an array of elements describing the resource. The first two indices are the width and height. The doc says that if there is no image or multiple images or some other file, the width and height will be 0. So you could check that:
$img = getimagesize($url);
if($img[0] === 0)
$imgFound = false;
Or you could check the third index (# 2), which is the filetype. To check this, you pare it against the GD constants:
if($img[2] === IMG_JPG || $img[2] === IMG_JPEG)
$imgFound = true;
Just make sure it's installed (check phpInfo()
). Edit: The docs however say that the GD extension doesn't need to be installed in order to use getimagesize
, but the constants will only be available if you install it.
Original answer:
As I said in my ment, there's no perfect way to do this if you don't have control over the server hosting the image, since it can respond to your request however it likes (reject it, redirect, etc...). But you can use file_get_contents
to get the file (which returns false
on failure) and then check the headers using $http_response_header
to see if it's an image. Here's an example script:
<?php
$url = 'http://www.example./example.jpg';
// some servers will do a redirect to a valid image page,
// so you don't want to reject a 302 necessarily.
$okCodes = array("200", "302");
$imgFound = false;
$pic = file_get_contents($url);
if($pic !== false) {
if(isset($http_response_header[0])) {
$firstHeader = explode(" ", $http_response_header[0]);
if(isset($firstHeader[1]) and in_array($firstHeader[1], $okCodes)) {
$count = count($http_response_header);
for($j = 0; $j < $count; ++$j) {
if(strpos($http_response_header[$j], "Content-Type: image/jpeg") !== false
|| strpos($http_response_header[$j], "Content-Type: image/jpg") !== false) {
$imgFound = true;
}
}
}
}
}
// and if you're using ajax, echo it back to your app
if($imgFound) {
echo json_encode(array("imgFound" => true));
} else {
echo json_encode(array("imgFound" => false));
}
?>
One way to improve this script would be to find the LAST "HTTP/1.1 [ERROR_CODE] [ERROR_MESSAGE]" header in $http_response_header
(which may contain multiple sets of headers - all merged into one array), which would allow you to see the headers for the file you're actually being served.