Upload image using php 7 best practices to make it secure I have a script that uploads an image, inserts it into database,  and displays it on the home page. 

However it's not secure, how can i make this script more secure and make sure it allows only jpeg/jpg files. 

UploadController.php

public function upload_image($filename, $image_name)
{

    $this-&gt;uploadfile = $this-&gt;directory . basename($filename);

    try{
        $sql = "INSERT INTO images (img, image_name) VALUES (?, ?)";
        $stmt = $this-&gt;connect-&gt;prepare($sql)-&gt;execute([$filename, $image_name]);
        return $stmt; 
    }
    catch(PDOExeception $e)
    {
        echo $e-&gt;getMessage();
    }

}


Home.php

&lt;?php
require_once 'paritals/header.php';
require_once 'paritals/nav.php';


use Eli\Db as DB;
use ElCont\UploadController as Image;


    if(isset($_FILES['profile_img'])){

        $dbh = new DB();
        $connect = $dbh-&gt;connect();
        $image = new Image($connect);

        $filename = $_FILES["profile_img"]["name"];
        $realname = "public/uploads/" . basename($filename);
        $directory = $_SERVER['DOCUMENT_ROOT'] . "/public/uploads/";

        $path = $directory . basename($filename);

        $image_name = $_POST['image_name'];


        if($image-&gt;upload_image($realname, $image_name)){

            move_uploaded_file($_FILES["profile_img"]["tmp_name"], $path);

            $owl = $image-&gt;get_image();


        }

}?&gt;

&lt;div class="container"&gt;
    &lt;div class="row"&gt;
        &lt;div class="col-md-6 offset-md-4 p-5 "&gt;
            &lt;h1&gt;Upload Image&lt;/h1&gt;
            &lt;form action="" enctype="multipart/form-data" method="post"&gt;
                &lt;div class="form-group"&gt;
                    &lt;label for="exampleInputFile"&gt;File input&lt;/label&gt;
                    &lt;input type="file" name="profile_img" class="form-control-file" id="exampleInputFile" aria-describedby="fileHelp"&gt;
                    &lt;input type="text" name="image_name" placeholder="Enter Name Of File" class="mt-3"&gt;
                    &lt;small id="fileHelp" class="form-text text-muted"&gt;This is some placeholder block-level help text for the above input. It's a bit lighter and easily wraps to a new line.&lt;/small&gt;
                    &lt;button type="submit" class="btn btn-primary"&gt;Submit&lt;/button&gt;
                &lt;/div&gt;

            &lt;/form&gt;
        &lt;/div&gt;
    &lt;/div&gt;

&lt;div class="row p-5"&gt;
&lt;?php

$dbh = new DB();
$connect = $dbh-&gt;connect();
$image = new Image($connect);

$photo =  $image-&gt;get_images();
foreach($photo as $pic)

{?&gt;


            &lt;div class="col-md-4"&gt;
              &lt;h1&gt;&lt;?php echo $pic['image_name'];?&gt;&lt;/h1&gt;
                &lt;img src="&lt;?php echo $pic['img'];?&gt;" width="300" height="400"&gt;

            &lt;/div&gt;


&lt;?php }?&gt;
       &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;?php require_once  'paritals/footer.php';?&gt;
For some reason you aren't following suggestions have been given to you in the other post. So again:

One of insecurities is revealing the internal error message to a site user. Remove that try and catch stuff from the database interaction and configure your server to report errors appropriately like you've been told.

Same goes for adding directory to the file name. Please remember, you already added it in the step before:

$realname = "uploads/" . basename($filename);


therefore, the following code makes no sense: 

$this-&gt;uploadfile = $this-&gt;directory . basename($filename);


Do you realize that?
Besides, a function responsible for storing a filename in the database should be dong only that - storing a filename. Assigning the filename or error reporting should be done elsewhere.

It means your upload image method should be exactly like this:

public function store($filename, $image_name)
{
    $sql = "INSERT INTO images (img, image_name) VALUES (?, ?)";
    $this-&gt;connect-&gt;prepare($sql)-&gt;execute([$filename, $image_name]);
}


making it to do the only stuff it is intended for. 

Regarding security, the main point is to verify the file's extension. Your web-server will judge the file type by its extension so should you.

$allowed = ['jpg', 'jpeg', 'png', 'gif'];
$extension = pathinfo($_FILES['file']['name'],PATHINFO_EXTENSION);
if (in_array($extension, $allowed)) {
    // process your file
} else {
    // tell a user the extension is not allowed
}


regarding general purpose best practices. Looks like your namespaces are inconsistent. It seems you want something like that

use Eli\Db as DB;
use Eli\Controller\UploadController as Image;


and also you are supposed to implement PSR-4 autoload so you won't have to include class definitions manually