Secure upload script I am creating a social network that let's users upload a profile picture. I just want to know if this is a secure way of doing it. Thanks.
&lt;?php

include 'includes/header.php';
include 'includes/form_handlers/settings_handler.php';

//$userPic = ''; 
$date_time = date('Y-m-d_H-i-s');

if(!empty($userLoggedIn)) {

    if (isset($_FILES['fileToUpload'])) {
        $errors= array();
        $file_name = $_FILES['fileToUpload']['name'];
        $file_size = $_FILES['fileToUpload']['size'];
        $width = 1500;
        $height = 1500;
        $file_tmp = $_FILES['fileToUpload']['tmp_name'];
        $file_type = $_FILES['fileToUpload']['type'];
        $tmp = explode('.',$_FILES['fileToUpload']['name']);
        $file_ext=strtolower (end ($tmp));

        $extensions = array( &quot;jpeg&quot;, &quot;jpg&quot;, &quot;png&quot;, &quot;gif&quot;);

        if(in_array($file_ext,$extensions)=== false){

            $errors[]=&quot;extension not allowed, please choose a JPEG or PNG file.&quot;;
        }
 
        if ($file_size &gt; 8097152) {

            $errors[] = 'File size must be 2 MB';
        }
 
        if ($width &gt; 1500 || $height &gt; 1500) {

            echo&quot;File is to large&quot;;
        }

        if(!$errors) {

            $userPic = md5($_FILES[&quot;fileToUpload&quot;][&quot;name&quot;]) . $date_time . &quot; &quot; . $file_name;
            $profilePic = move_uploaded_file($file_tmp,&quot;assets/images/profile_pics/&quot; . $userPic);

            $file_path = &quot;assets/images/profile_pics/&quot; . $userPic;

            $stmt = $con-&gt;prepare(&quot;UPDATE users SET profile_pic = ? WHERE username = ?&quot;);
            $stmt-&gt;bind_param('ss', $file_path, $username);
            $stmt-&gt;execute();
            $stmt-&gt;close();

            header('Location: settings.php');
            exit();
 
        }
    }
} else {

    echo &quot;Invalid Username&quot;;
}

?&gt;
This is my personal opinion, but I'd say the following:

The code should be formatted, I'd personally look at PSR-12 as this standard should be followed when possible.
move_uploaded_file doesn't protect against directory traversal. You should use basename on $_FILES['fileToUpload']['tmp_name'] and some other forms of validation
Checking the file extension with if(in_array($file_ext,$extensions)=== false) doesn't prevent a user from uploading a malicious file they could for instance use a magic byte to trick the server into thinking it's a certain type of file. You should take a look at finfo and the first example on file upload
You're create an array of errors, currently that's being checked in an if statement and is then thrown away. If you aren't planning on using it you might be better just returning out of the function early rather than continuing execution.
Depending on how unique the filename should be you might want to use something like uniqid(mt_rand(), true)
move_uploaded_file will replace a file if it already exists, you might want to check that this exists before you overwrite an existing file. Depending on your naming solution it's very unlikely to occur but under high load for long periods of time this could happen more often than you think.
You're using UPDATE users SET profile_pic = ? WHERE username = ? I'd assume that this value exists in the database as the user needs to be logged in. However, if you aren't sure if the field exists or not (I haven't seen the database) I'd personally use: INSERT INTO users (profile_pic, username) VALUES (?,?) ON DUPLICATE KEY UPDATE profile_pic=?, username=? this will insert into the table if the row doesn't exist but will update it if it does.
You've set a local variable called width and height and are comparing them to the same value. I assume this was meant to check the actual file dimensions?

I hope this helps in some way :)