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
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]
Link to comment
Share on other sites

[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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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