I reworked the code and it works well now 🙂
I generate a random name for the file (32 characters) using a function in functions.tpl.php
here is the code:
<?php
require('../../includes/functions.tpl.php');
$uploaddir = "../../gifts/";
$allowed_ext = "jpg, gif, jpeg, png";
$max_size = "50000";
$max_height = "300";
$max_width = "300";
$upload = '';
//get the file's extension
$ext = pathinfo($_FILES['file']['name']);
$extension = $ext['extension'];
//insert the authorized extensions in an array
$allowed_paths = explode(", ", $allowed_ext);
//compare uploaded file with authorized extensions
if (in_array($extension,$allowed_paths))
{
//check the file's size in kb
if($FILES['file']['size'] > $max_size)
{
print "File size is too big!";
$upload = false;
}
//check the file's dimension WxH
if ($max_width && $max_height)
{
list($width, $height, $type, $w) = getimagesize($FILES['file']['tmp_name']);
if($width > $max_width || $height > $max_height)
{
print "<br>File height and/or width are too big!";
$upload = false;
}
//upload the file!
else
{
if(empty($upload) && is_uploaded_file($_FILES['file']['tmp_name']))
{
//create a random name for the uploaded file
$name = random_string();
$uploadname = "$name.$extension";
move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$uploadname);
print "Your file has been uploaded successfully! Yay!";
}
else
{
print "<br>failed uploading the file";
}
}
}
}
else
{
print "Incorrect file extension!";
}
?>