I have a PHP script that uploads an image file, checks it for size, extension, and type. It then converts it to png, saves it to a folder outside the web root with a randomly generated name, then moves it to a specific folder inside the web root with a new filename that matches the user's username. By default, users have a generic profile image, but if they use the script to change their picture the database is updated to reflect the new filename. So far it's working without any obvious problems, but I would just like to have the script reviewed for possible issues. Also, what changes should be made to make it more secure?
<div class="formSection">
<?php
if(isset($_FILES["image"]["name"])) {
$imageFile = ($_FILES["image"]["name"]);
$imageType = ($_FILES["image"]["type"]);
$imageSize = ($_FILES["image"]["size"]);
$validext = array("jpeg","jpg","gif","png");
$fileExt = pathinfo($imageFile, PATHINFO_EXTENSION);
$fileExt = strtolower($fileExt);
$imageErrors = array();
if($_FILES["image"]["size"] > 1048576) {
$imageErrors[] = "<strong>ERROR!</strong> File size was too large! It must be under 1MB.";
}
if((($imageType == "image/jpeg") || ($imageType == "image/jpg") || ($imageType == "image/gif") || ($imageType == "image/png")) && in_array($fileExt, $validext)) {
}else{
$imageErrors[] = "<strong>ERROR!</strong> Not an image file! Only jpg, gif, and png allowed.";
}
if($_FILES["image"]["error"]) {
$imageErrors[] = "<strong>ERROR!</strong> Looks like there was an error. " . $_FILES["image"]["error"];
}
if(empty($imageErrors)) {
if(exif_imagetype($_FILES['image']['tmp_name']) == IMAGETYPE_GIF) {
$newpng = 'gifto.png';
$png = imagepng(imagecreatefromgif($_FILES['image']['tmp_name']), $newpng);
}
elseif(exif_imagetype($_FILES['image']['tmp_name']) == IMAGETYPE_JPEG) {
$newpng = 'jpgto.png';
$png = imagepng(imagecreatefromjpeg($_FILES['image']['tmp_name']), $newpng);
}
elseif(exif_imagetype($_FILES['image']['tmp_name']) == IMAGETYPE_PNG) {
$newpng = '.png';
$png = imagepng(imagecreatefrompng($_FILES['image']['tmp_name']), $newpng);
}
$temporaryPath = "../../images/" . uniqid() . $newpng;
$sourcePath = $_FILES["image"]["tmp_name"];
move_uploaded_file($sourcePath, $temporaryPath);
$profilePictureMessage = ' <div class="alert alert-success">
<strong>SUCCESS!</strong> Image file uploaded!
</div>' . chr(13) . chr(10);
$finalPath = "images/profilePictures/" . $username . $newpng;
rename($temporaryPath, $finalPath);
$query = $con->prepare("UPDATE users SET profilePic = :profilePic WHERE username=:un");
$query->bindParam(":profilePic", $finalPath);
$query->bindParam(":un", $username);
$query->execute();
unlink($newpng);
}else{
foreach($imageErrors as $imageError) {
$profilePictureMessage = ' <div class="alert alert-danger">'
.$imageError.
'</div>' . chr(13) . chr(10);
}
}
}
?>
<div class="message">
<?php echo $profilePictureMessage; ?>
</div>
<form action="settings.php" method="POST" enctype="multipart/form-data">
<span class="title">Change profile picture</span>
<div class="form-group">
<input type="file" class="form-control-file" name="image" accept="image/png, image/gif, image/jpeg">
</div>
<button type="submit" class="btn btn-primary" name="saveProfilePictureButton">Save</button>
</form>
</div>
I welcome any and all feedback, and thanks for your time.