note 37400 added to function.fread

From: Date: Thu, 13 Nov 2003 18:50:51 +0000
Subject: note 37400 added to function.fread
Groups: php.notes 
Request: Send a blank email to php-notes+get-60349@lists.php.net to get a copy of this message
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; } } ---- Manual Page -- http://www.php.net/manual/en/function.fread.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+37400 Delete -- http://master.php.net/manage/user-notes.php?action=delete+37400&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+37400&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#60349) next »