note 37400 deleted from function.fread by vincent
| From: | vincent@php.net | Date: | Sat, 15 Nov 2003 22:20:50 +0000 |
| Subject: | note 37400 deleted from function.fread by vincent | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-60435@lists.php.net to get a copy of this message | ||
Note Submitter: roger_dupere@hotmail.com
----
here is a script i have created to check if a image file is REALLY an image file.
function is_image( $imgpath ) {
define( "ERR_FILENOTEXISTS", 0 );
define( "ERR_FILENOTREADABLE", 1 );
define( "ERR_FILETYPENOTGIF", 2 );
define( "ERR_FILETYPENOTJPG", 3 );
define( "ERR_FILETYPENOTBMP", 4 );
define( "ERR_FILETYPENOTIMG", 5 );
if( file_exists( $imgpath ) ) {
if( is_readable( $imgpath ) ) {
$fp = fopen( $imgpath, "rb" );
$fileext = strtoupper( substr( $imgpath, strrpos( $imgpath, "." ) + 1 ) );
switch( $fileext ) {
case "GIF":
$type = fread( $fp, 5 ); //Read the first 5 bytes for a gif file
fclose( $fp );
if( $type == "GIF89a" ) {
return "GIF";
} else {
return ERR_FILETYPENOTGIF;
}
case "JPG":
$type = bin2hex( fread( $fp, 2 ) ); //Read the first 2 bytes for a jpeg file
fclose( $fp );
if( $type == "ffd8" ) {
//ffd8 = Signature for the JPEG file
return "JPG";
} else {
return ERR_FILETYPENOTJPG;
}
case "JPEG":
$type = bin2hex( fread( $fp, 2 ) ); //Read the first 2 bytes for a jpeg file
fclose( $fp );
if( $type == "ffd8" ) {
//ffd8 = Signature for the JPEG file
return "JPEG";
} else {
return ERR_FILETYPENOTJPEG;
}
case "BMP":
$type = bin2hex( fread( $fp, 2 ) ); //Read the first 2 bytes for a bmp file
fclose( $fp );
if( $type == "424d" ) {
//424d = Signature for the Windows BMP file
return "WINDOWS_BMP";
}
if( $type == "4d42" ) {
//4d42 = Signature for the OS/2 BMP file (Not confirmed).
return "OS2_BMP";
}
return ERR_FILETYPENOTBMP;
}
return ERR_FILETYPENOTIMG;
} else {
return ERR_FILENOTREADABLE;
}
} else {
return ERR_FILENOTEXISTS;
}
}