I have written a code which should check whether the file size exceeds 8.5 MB or not & if it does, it should produce and error and also prohibit the post from entering into the DB. The code is prohibiting the post to enter the DB but it is not showing any error stating that the file size exceeds. ( P.S: The check for Unknown File Format is working.) Here is the code i have written:
//$session id
define ("MAX_SIZE","9000");
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$valid_formats = array("jpg", "png", "gif", "bmp","jpeg");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$uploaddir = "uploads/"; //a directory inside
foreach ($_FILES['photos']['name'] as $name => $value)
{
$filename = stripslashes($_FILES['photos']['name'][$name]);
$size=filesize($_FILES['photos']['tmp_name'][$name]);
//get the extension of the file in a lower case format
$ext = getExtension($filename);
$ext = strtolower($ext);
if(in_array($ext,$valid_formats))
{
if ($size < (MAX_SIZE*1024))
{
$image_name=time().$filename;
echo "<img src='".$uploaddir.$image_name."' class='imgList'>";
$newname=$uploaddir.$image_name;
if (move_uploaded_file($_FILES['photos']['tmp_name'][$name], $newname))
{
$time=time();
mysql_query("INSERT INTO user_uploads(image_name,user_id_fk,created) VALUES('$image_name','$session_id','$time')");
}
else
{
echo '<p style="color: Red;">You have exceeded the size limit! so moving unsuccessful! </p>';
}
}
else
{
echo '<p style="color: Red;">You have exceeded the size limit!</p>';
}
}
else
{
echo '<p style="color: Red;">Unknown extension!</p>';
}
}
}