Jump to content

[SOLVED] string not equal to array element, whats the operator


Lodius2000

Recommended Posts

heres what i have, trying to error check that an uploaded file has a jpg extension heres what i got

just drawing a blank on the operator, please check my array syntax too please

 

<?php
$str = '1111111111.jpg'; //would contain uploaded file's name
list($file, $ext) = explode('.', $str);
print "$file is file<br /> $ext is ext<br />";//for error checking purposes

$extentions = array(0 => 'jpg',
		      1 => 'jpeg');

if ($ext != $extentions){
print "gimme jpg please!";
} else {
print "thats a jpg";
}
?>

 

also is there a way to actually read the file and determine if its a particular type so that someone cant upload a tiff called blah.jpg?

The failure is due to the fact that $ext is a value and $extensions is an array.

 

What you would need is "!in_array ($ext, $extensions)" and not "$ext != $extensions".

 

Apart from that, you're better off using regex for this:

 

$extensions = array ("jpg", "jpeg");

 

if (!preg_match ("/\.(".implode ("|", $extensions).")$/", $file)) // where $file is the fullname of the file

joquius

i was thinking regex but i am terrible at it so thanks for the code, will definatly work better that what i got

 

edit: so heres another question, how do you get the name of the file, or can i just use $_POST['uploaded_file'], does that store the filename in addition to the file data? or is there something else I need to do to get the name

verifying the extension is less secure then actually open the image and reading some data to verify its content is a valid image file

 

I can make a file that is really an executable in nature but call it a .jpg and sneak by your test

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.