checking if an image exists

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wyrmmage
Forum Commoner
Posts: 56
Joined: Sat Oct 28, 2006 12:43 pm
Location: boise, ID

checking if an image exists

Post by wyrmmage »

Hello everyone :)
I need a function/code/a hint in the right direction as to finding out if an image resides of the server that I'm running my php code from. Could I just use fopen() to check if it was there, or is there a better way?
Thanks in advance :)
-wyrmmage
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

wyrmmage
Forum Commoner
Posts: 56
Joined: Sat Oct 28, 2006 12:43 pm
Location: boise, ID

Post by wyrmmage »

oh, lol, ok...that should work quite well :)
Thanks for the help guys,
-wyrmmage
brendandonhue
Forum Commoner
Posts: 71
Joined: Mon Sep 25, 2006 3:21 pm

Post by brendandonhue »

You can getimagesize() if you want to make sure it's an image.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

is_readable is good too
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Post by Jaxolotl »

file_exists() check if is it really a file, but make not distintion between file types.
For example if you "feed" the funtion with only a directory path it will return TRUE if the directory exists.

So you may create a funtion that returns true if it find the file type you want as on the example below
the file_exists() function + Pearl style pattern match

http://it2.php.net/manual/en/function.preg-match.php

Code: Select all

function isImageAndExists($string){
	if((file_exists("$string"))&&(preg_match('#(\.jpg|\.jpeg|\.gif|\.png|\.bmp)$#i',$string))){
		return true;
	}
	else{
		return false;
	}
}
if it not enough self explained just ask

anyway the getimagesize() suggested by brendandonhue is a good option
brendandonhue
Forum Commoner
Posts: 71
Joined: Mon Sep 25, 2006 3:21 pm

Post by brendandonhue »

If you want to match the file extension, you could just use pathinfo(). But that doesn't really tell you if it's an image, since you could rename a .exe to .jpg. getimagesize() actually pulls the first few bytes of the file and checks it against known file formats.
wyrmmage
Forum Commoner
Posts: 56
Joined: Sat Oct 28, 2006 12:43 pm
Location: boise, ID

Post by wyrmmage »

Ok, thanks for the help :)
Right now I've got the function:

Code: Select all

function displayImage($page_name,$image_number)
{

//Define the Search query, to fetch information
$SearchImages = mysql_query("
SELECT *
FROM `images`
WHERE
`page` = '$page_name'
");

if(!$SearchImages) die(mysql_error());

$images = mysql_fetch_array($SearchImages);

$imageLinks = explode(":", $images['links']);

$imageLink = $imageLinks[$image_number];

$imgSource = 'images/' . $imageLink;

if(fopen($imgSource, 'r'))
{
echo('
<IMG SRC="' . $imgSource . '" border="0">
');
}
else
{
$imageParts = explode(".", $imageLink);

if($imageParts[1] == 'JPG')
{
$imageParts[1] = 'jpg';
}
else if($imageParts[1] == 'jpg')
{
$imageParts[1] = 'JPG';
}
else if($imageParts[1] == 'BMP')
{
$imageParts[1] = 'bmp';
}
else if($imageParts[1] == 'bmp')
{
$imageParts[1] = 'BMP';
}
else if($imageParts[1] == 'GIF')
{
$imageParts[1] = 'gif';
}
else if($imageParts[1] == 'gif')
{
$imageParts[1] = 'GIF';
}

$imgSource = 'images/' . $imageParts[0] . '.' . $imageParts[1] . '';
if(fopen($imgSource, 'r'))
{

echo('
<IMG SRC="' . $imgSource . '" border="0">
');

}

}

}
This seems to work pretty well, although I might change fopen() to one of the functions that you listed :)
Thanks again guys ^_^
-wyrmmage
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Make a separate function for displaying like this:

Code: Select all

function imageDisplay($path, $alt = '', $border = 0)
{
    return '<img src="' . $path . '" alt="' . $alt . '" border="' .$border . '" />';
}
and change your displayImage to imageGet or something like that.
Post Reply