Jump to content


Photo

valladate file type via array


  • Please log in to reply
7 replies to this topic

#1 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 13 August 2006 - 11:10 AM

can you think off a better way to valadate a file type please cheers.
<?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";

}

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 13 August 2006 - 11:43 AM

<?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");
}
?>

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 13 August 2006 - 11:47 AM

orio that not programming that's scince lol...................

please exsplain all please cheers?
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 13 August 2006 - 01:01 PM

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

<?
  $file = 'xxx.gif';
  
  $ext = strrchr($file, '.');
  
  echo $ext;  //---> .gif
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 August 2006 - 01:50 PM

You can also use the pathinfo() function (http://www.php.net/pathinfo)
<?php
$file = 'somefile.jpg';
list ($dir,$base,$extension) = $file;
echo $extension;
?>

Ken

#6 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 13 August 2006 - 01:57 PM

orio that not programming that's scince lol...................

please exsplain all please cheers?

Ok, here is the script with an explantion:

<?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");
}
?>

Orio.
Think you're smarty?

(Gone until 20 to November)

#7 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 13 August 2006 - 08:56 PM

What if my file was

supervirusgifjpgpng.exe.gif

#8 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 13 August 2006 - 08:58 PM

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.
Think you're smarty?

(Gone until 20 to November)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users