I have an input(file)-field that a user can upload an image from. I would like to check the file format of the file that the user tries to upload before I upload it to my server, but this function doesn't do the job(and I can't understand why):
$info = Getimagesize($form_data);
if($info[2]<3){
write_file($form_data, $bilde, $user);
} else echo "Picture is not JPEG or GIF";
This is the write_file-function(if it has anything to do with my problem):
function write_file($form_data, $bilde, $user){
$filename = "Users/".$user."/userdata/".$user.".jpg";
$brukerbilde = fread(fopen($form_data, "r"), filesize($form_data));
$fs = filesize($form_data);
$ft = Getimagesize($form_data);
if($ft[2] == 1)
$ftype = '.jpg';
else if($ft[2] == 2)
$ftype = '.gif';
else
$ftype = 'unknown format';
if (!$handle = fopen($filename, 'w+')) {
print "Cannot open file ($filename)";
exit;
}
if (!fwrite($handle, $brukerbilde)) {
print "Cannot write to file ($filename)";
exit;
}
$sql_p = "REPLACE INTO Pictures (fileName, filesize, filetype, username) VALUES ('$filename', '$fs', '$ftype', '$user')";
$var_p = mysql_query($sql_p) or die("jalla: " . mysql_error());
//print "Success, wrote to file ($filename)";
fclose($handle);
}
As you can see, I upload my image directly to my server, and I save the path to the picture, the size of the picture and a textual format of the picture. I only want the users to be able to upload .jpg's and .gif's
Anyone know why this thing let's me upload any sort of file to the server ???
---desperate for help---