I am searching that for at least half an hour but I am not able to figure it out what the issue with my code?
Here it is:
<?php
$file = $_FILES["file"];
$filename = $_FILES["file"]["name"];
$tempdir = $_FILES["file"]["tmp_name"];
$error = $_FILES["file"]["error"];
$type = $_FILES["file"]["type"];
$size = $_FILES["file"]["size"];
$maxsize = 524288;
$allowedtypes = array("image/png", "image/jpg", "image/jpeg", "image/bmp");
$errormsg = "";
if(!empty($error))
{
switch($error)
{
case '1':
$errormsg = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
break;
case '2':
$errormsg = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
break;
case '3':
$errormsg = 'The uploaded file was only partially uploaded';
break;
case '4':
$errormsg = 'No file was uploaded.';
break;
case '6':
$errormsg = 'Missing a temporary folder';
break;
case '7':
$errormsg = 'Failed to write file to disk';
break;
case '8':
$errormsg = 'File upload stopped by extension';
break;
default:
$errormsg = 'No error code avaiable';
}
} elseif(empty($tempdir) || $tempdir == 'none') {
$errormsg = 'No file was uploaded..';
} elseif(!in_array($type, $allowedtypes) || $size > $maxsize) {
$errormsg = 'Either image type not supported or size is extending 512 KB';
} else {
$filename = $_SESSION["username"];
$extension = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);
$path = 'profile_pictures/' . $filename . "." . $extension;
foreach($ext as $allowedtypes) {
if(file_exists($filename . "." . $ext)) unlink($filename . "." . $ext);
}
move_uploaded_file($tempdir, $path);
//for security reason, we force to remove all uploaded file
@unlink($file);
}
echo $path;
?>
If I remove the foreach loop it works but it's not working with that!
Why is that happening? Please help.