On my website, i allow users to submit a profile picture and i check if the picture is "png or jpeg" and also i check if the file is less than "2 mb" and if it is, i display an error message. But when the file is less than the php ini upload max, it display the message but when it is greater i get a lot of php errors that is not what i wanted to display. How do i remove the errors and display my generated message to the user. I found a post but the post was how to read the errors. I only get those errors when the submitted file is greater than 200mb or the php ini upload max. When its lower, it goes fine. Here is my code and error messages
PHP
<?php
session_start();
if(isset($_COOKIE['username'])){
if($_SESSION['came_from_upload'] != true){
setcookie("username", "", time() - 60*60);
$_COOKIE['username'] = "";
header("Location: developerLogin.php");
exit;
}
if($_SERVER['REQUEST_METHOD'] =="POST"){
$userid = $_SESSION['id'];
$fullname = addslashes(trim($_POST['fullname']));
$username = addslashes(trim($_POST['username']));
$email = addslashes(trim($_POST['email']));
$password = addslashes(trim($_POST['password']));
$storePassword = password_hash($password, PASSWORD_BCRYPT, array('cost' => 10));
$file_name = addslashes(trim($_FILES['file']['name']));
$file_tmp = addslashes(trim($_FILES['file']['tmp_name']));
try{
// new php data object
$handler = new PDO('mysql:host=127.0.0.1;dbname=magicsever', 'root', '');
//ATTR_ERRMODE set to exception
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
die("There was an error connecting to the database");
}
$stmtChecker = $handler->prepare("SELECT * FROM generalusersdata WHERE user_id = ?");
$stmtChecker->execute(array($userid));
if(!$stmtChecker->fetch()){
setcookie("username", "", time() - 60*60);
$_COOKIE['username'] = "";
header("Location: developerLogin.php");
exit;
}
if(!empty($fullname)){
$stmtFullname = $handler->prepare("UPDATE generalusersdata SET fullname = ? WHERE user_id = ?");
$stmtFullname->execute(array($fullname, $userid));
}
if(!empty($username)){
$stmtCheckerUsername = $handler->prepare("SELECT * FROM generalusersdata WHERE username = ?");
$stmtCheckerUsername->execute($username);
if($resultCheckerUsername = $stmtCheckerUsername->fetch()){
die("Username Already in use! Please try again");
}
$stmtUsername = $handler->prepare("UPDATE generalusersdata SET username = ? WHERE user_id = ?");
$stmtUsername->execute(array($username, $userid));
}
if(!empty($email)){
if(filter_var($email, FILTER_VALIDATE_EMAIL) == false){
die ("Email is Not Valid!");
}
$stmtCheckerEmail = $handler->prepare("SELECT * FROM generalusersdata WHERE email = ?");
$stmtCheckerEmail->execute($email);
if($resultCheckerEmail = $stmtCheckerEmail->fetch()){
die("Email Already in use! Please try again");
}
$stmtEmail = $handler->prepare("UPDATE generalusersdata SET email = ? WHERE user_id = ?");
$stmtEmail->execute(array($email, $userid));
}
if(!empty($password)){
if(strlen($password) < 6){
die ("Password has to be GREATER than 6 characters!");
}
//Check if password has atleast ONE Uppercase, One Lowercase and a number
if(!preg_match("(^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$)",$password)){
echo 'Password needs to be at least ONE uppercase, ONE lowercase, and a number!';
exit;
}
$stmtPassword = $handler->prepare("UPDATE generalusersdata SET password = ? WHERE user_id = ?");
$stmtPassword->execute(array($storePassword, $userid));
}
if($_FILES['file']['error'] == UPLOAD_ERR_OK){
$allowedTypes = array(IMAGETYPE_PNG, IMAGETYPE_JPEG);
$detectedType = exif_imagetype($_FILES['file']['tmp_name']);
if($extensionCheck = !in_array($detectedType, $allowedTypes) || $_FILES['file']['size'] < 2000){
die("Failed to upload image; the format is not supported");
}
$dir = "userprofilepicture";
if(is_dir($dir)==false){
mkdir($dir, 0700);
}
move_uploaded_file($file_tmp,$dir.'/'.$file_name);
$stmtPassword = $handler->prepare("UPDATE generalusersdata SET profile_image = ? WHERE user_id = ?");
$stmtPassword->execute(array($file_name, $userid));
}
echo "ok";
}
}else{
header("Location: developerLogin.php");
exit;
}
?>