TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Having trouble with Custom Function "isImage" (http://www.talkphp.com/general/1776-having-trouble-custom-function-isimage.html)

Orc 12-20-2007 01:52 AM

Having trouble with Custom Function "isImage"
 
PHP Code:

<?php

 
function isImage($handler) {
    if(
file_exists($handler)) {
        
$handler == $handler.".JPG";
        
$handler == $handler.".PNG";
        
$handler == $handler.".GIF";
        
$handler == $handler.".BMP";
 }
}

$handler "test.bmp";

 if(
isImage($handler)) {
     echo 
"$handler is in fact, an Image!";
} else 
   echo 
"$handler is not an Image!";

   

?>

It outputs $handler is not an Image when really it is!

What is the problem here!

One more thing, is it case sensitive?

Andrew 12-20-2007 02:15 AM

What are you trying to accomplish with the following?
PHP Code:

$handler == $handler.".JPG";
$handler == $handler.".PNG";
$handler == $handler.".GIF";
$handler == $handler.".BMP"

If you are trying to do what I think you are doing, this should suffice.
PHP Code:

function isImage($handler) {
    
/* Make the string all lowercase */
    
$handler strtolower($handler);
    
/* Get the extension */
    
$extension explode('.'$handler);
    
$extension array_splice($extension, -11);
    
/* Check if filetype is allowed */
    
if ($extension[0] == 'jpg' || $extension[0] == 'png' || $extension[0] == 'gif' || $extension[0] == 'bmp') {
        
/* Check if the file exists */
        
if (file_exists($handler)) {
            echo 
'File exists.';
        } else {
            echo 
'File does not exist.';
        }
    } else {
        echo 
'Unknown file type.';
    }



Orc 12-20-2007 02:34 AM

Quote:

Originally Posted by Andrew (Post 6894)
What are you trying to accomplish with the following?
PHP Code:

$handler == $handler.".JPG";
$handler == $handler.".PNG";
$handler == $handler.".GIF";
$handler == $handler.".BMP"

If you are trying to do what I think you are doing, this should suffice.
PHP Code:

function isImage($handler) {
    
/* Make the string all lowercase */
    
$handler strtolower($handler);
    
/* Get the extension */
    
$extension explode('.'$handler);
    
$extension array_splice($extension, -11);
    
/* Check if filetype is allowed */
    
if ($extension[0] == 'jpg' || $extension[0] == 'png' || $extension[0] == 'gif' || $extension[0] == 'bmp') {
        
/* Check if the file exists */
        
if (file_exists($handler)) {
            echo 
'File exists.';
        } else {
            echo 
'File does not exist.';
        }
    } else {
        echo 
'Unknown file type.';
    }



Thanks, I'll try it! :D

deflated 12-29-2007 05:18 PM

The extension of a file does not prove that it contains an image. Even better is to determine the MIME type or to use exif_imagetype() and check if it has returned false.

Alan @ CIT 12-30-2007 12:19 AM

For getting the MIME type, I would suggest using getimagesize():

PHP Code:

$myImage getimagesize('images/pic.gif');
echo 
$myImage['mime'];

// would echo the mime type for the given image 

Alan


All times are GMT. The time now is 09:47 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0