Jump to content

valladate file type via array


redarrow

Recommended Posts

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]
Link to comment
https://forums.phpfreaks.com/topic/17401-valladate-file-type-via-array/
Share on other sites

Checking the extension is one way but that could have been altered. If it's an image file

try

getimagesize()

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 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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.