TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-20-2007, 01:52 AM   #1 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default 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?
Orc is offline  
Reply With Quote
Old 12-20-2007, 02:15 AM   #2 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default

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.';
    }

Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
The Following User Says Thank You to Andrew For This Useful Post:
Orc (12-20-2007)
Old 12-20-2007, 02:34 AM   #3 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Andrew View Post
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
Orc is offline  
Reply With Quote
Old 12-29-2007, 05:18 PM   #4 (permalink)
The Wanderer
 
deflated's Avatar
 
Join Date: Dec 2007
Location: 127.0.0.1
Posts: 19
Thanks: 7
deflated is on a distinguished road
Default

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.
deflated is offline  
Reply With Quote
Old 12-30-2007, 12:19 AM   #5 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

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
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:16 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design