Jump to content

Archived

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

redarrow

valladate file type via array

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]

Share this post


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

Share this post


Link to post
Share on other sites
orio that not programming that's scince lol...................

please exsplain all please cheers?

Share this post


Link to post
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]

Share this post


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

Share this post


Link to post
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.

Share this post


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

Share this post


Link to post
Share on other sites

×

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.