redarrow Posted August 13, 2006 Share Posted August 13, 2006 can you think off a better way to valadate a file type please cheers.[code]<?php$file="redarrow.gif";$file_type=eregi_replace("^[a-zA-Z]{1,100}","",$file);$a=array(".php",".jpg");if(in_array($file_type,$a)){echo " correct file type";}else{echo "sorry wrong file type";}?>[/code] Quote Link to comment Share on other sites More sharing options...
Orio Posted August 13, 2006 Share Posted August 13, 2006 [code]<?php$file="redarrow.inc.php";$types_allowed=array("php","jpg");$explode=explode(".",$file);if(in_array($explode[count($explode)-1],$types_allowed)){echo("File is valid");}else{echo("Invalid file type");}?>[/code]Orio. Quote Link to comment Share on other sites More sharing options...
redarrow Posted August 13, 2006 Author Share Posted August 13, 2006 orio that not programming that's scince lol...................please exsplain all please cheers? Quote Link to comment Share on other sites More sharing options...
Barand Posted August 13, 2006 Share Posted August 13, 2006 Checking the extension is one way but that could have been altered. If it's an image filetrygetimagesize()Returns an array with 4 elements. Index 0 contains the width of the image in pixels. Index 1 contains the height. Index 2 is a flag indicating the type of the image: 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(intel byte order), 8 = TIFF(motorola byte order), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF, 15 = WBMP, 16 = XBM.If checking the extension, try strrchr().[code]<? $file = 'xxx.gif'; $ext = strrchr($file, '.'); echo $ext; //---> .gif?>[/code] Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted August 13, 2006 Share Posted August 13, 2006 You can also use the pathinfo() function (http://www.php.net/pathinfo)[code]<?php$file = 'somefile.jpg';list ($dir,$base,$extension) = $file;echo $extension;?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
Orio Posted August 13, 2006 Share Posted August 13, 2006 [quote author=redarrow link=topic=104070.msg414964#msg414964 date=1155469657]orio that not programming that's scince lol...................please exsplain all please cheers?[/quote]Ok, here is the script with an explantion:[code]<?php//a random file name, can contain any set of chars$file="redarrow.inc.php";//here are the extensions allowed, w/o a dot before$types_allowed=array("php","jpg");//create an array from the string,//splitting it by a dot. In our case//the out put will be://0=>redarrow, 1=>inc, 2=inc$explode=explode(".",$file);//Check if the last element in our//array (which is the extension)//is a valid extension (if it's in $types_allowed)if(in_array($explode[count($explode)-1],$types_allowed)){echo("File is valid");}else{echo("Invalid file type");}?>[/code]Orio. Quote Link to comment Share on other sites More sharing options...
extrovertive Posted August 13, 2006 Share Posted August 13, 2006 What if my file wassupervirusgifjpgpng.exe.gif Quote Link to comment Share on other sites More sharing options...
Orio Posted August 13, 2006 Share Posted August 13, 2006 It would see if gif is in the array. And it really is a gif.See the example in my script, I used $file="redarrow.inc.php";Orio. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.