First, make a database and connect it to your project.

create a folder include and create a file name database.php and function.inc.php.

database.php 

session_start();

$host="localhost"; // Host name

$db_username="acti_abhij"; // Mysql username

$db_pass="Simple"; // Mysql password

$db_name="acti_abhi"; // Database name

$conn = new mysqli($host, $db_username, $db_pass, $db_name);

if(!$conn){

    echo "Not Connect";

}

$timezone = "Asia/Kolkata";

date_default_timezone_set($timezone);

If you already have a login system then use session_start(); on the database.php file on top, otherwise you can skip it. 

Now create a file update_profile_image.php on the root directory for the image uploading HTML part.


update_profile_image.php

             

 

           

 

                   

                        method="post" enctype="multipart/form-data">

                        

                       

 

                           

Choose Image file:

 

                           

 

                                

                           

 

                       

 

                       

 

                            

                                value="Upload">

                       

 

                   

 

                   

                   

">

 

                   

           

 

 </p><p>if ( window.history.replaceState ) {</p><p>  window.history.replaceState( null, null, window.location.href );</p><p>}</p><p>
 

Include database.php and function.inc.php files on update_profile_image.php file like below.

 

 

Below code for css part.

</p><p>#frm-image-upload{</p><p>    padding: 0px;</p><p>    background-color: #fff;</p><p>}</p><p>.form-row {</p><p>    padding: 20px;</p><p>    /* border-top: #8aacb7 1px solid; */</p><p>}</p><p>.button-row {</p><p>    padding: 10px 20px;</p><p>    border-top: #8aacb7 1px solid;</p><p>}</p><p>#btn-submit {</p><p>    padding: 10px 40px;</p><p>    background: #586e75;</p><p>    border: #485c61 1px solid;</p><p>    color: #FFF;</p><p>    border-radius: 2px;</p><p>}</p><p>.file-input {</p><p>    background: #FFF;</p><p>    padding: 5px;</p><p>    margin-top: 5px;</p><p>    border-radius: 2px;</p><p>    border: #8aacb7 1px solid;</p><p>}</p><p>.response {</p><p>    padding: 10px;</p><p>    margin-top: 10px;</p><p>    border-radius: 2px;</p><p>}</p><p>.error {</p><p>    background: #fdcdcd;</p><p>    border: #ecc0c1 1px solid;</p><p>}</p><p>.success {</p><p>    background: #c5f3c3;</p><p>    border: #bbe6ba 1px solid;</p><p>}</p><p>

Note: window.history.replaceState we are using because we do not want to upload the same file while page refreshes. it will prevent page refresh.

Now we will write php code for image uploading.

function.inc.php

/Profile Upload Empoyee start

if (isset($_POST["upload"])) {

    $hrid = $_POST['hrid'];

    // Get Image Dimension

    $fileinfo = @getimagesize($_FILES["file-input"]["tmp_name"]);

    $width = $fileinfo[0];

    $height = $fileinfo[1];

    $allowed_image_extension = array(

        "png",

        "jpg",

        "jpeg"

    );

    // Get image file extension

    $file_extension = pathinfo($_FILES["file-input"]["name"], PATHINFO_EXTENSION);

    // Validate file input to check if is not empty

    if (! file_exists($_FILES["file-input"]["tmp_name"])) {

        $response = array(

            "type" => "error",

            "message" => "Choose image file to upload."

        );

    }    // Validate file input to check if is with valid extension

    else if (!in_array($file_extension, $allowed_image_extension)) {

        $response = array(

            "type" => "error",

            "message" => "Upload valiid images. Only PNG and JPEG are allowed."

        );

      //  echo $result;

    }    // Validate image file size

    else if (($_FILES["file-input"]["size"] > 2000000)) {

        $response = array(

            "type" => "error",

            "message" => "Image size exceeds 2MB"

        );

    }    // Validate image file dimension

    else if ($width > "300" || $height > "300") {

        $response = array(

            "type" => "error",

            "message" => "Image dimension should be within 300X300"

        );

    } else {

        $target = "profile_image/" .time()."-". basename($_FILES["file-input"]["name"]);

        if (move_uploaded_file($_FILES["file-input"]["tmp_name"], $target)) {

            $result1 = mysqli_query($conn,"SELECT `profile_image` FROM `tbl_hr` WHERE `hr_usr_id`='$hrid'");

            while($data=mysqli_fetch_array($result1)){

                $delete=$data['profile_image'];

                unlink($delete);

                //unlink("../profile_image/$delete");

            }

            mysqli_query($conn,"UPDATE `tbl_hr` SET `profile_image`='$target' WHERE `hr_usr_id`='$hrid'");

            $response = array(

                "type" => "success",

                "message" => "Image uploaded successfully."

            );

        } else {

            $response = array(

                "type" => "error",

                "message" => "Problem in uploading image files."

            );

        }

    }

}

//Profile Upload Empoyee End

Note: mysqli_query($conn,"UPDATE `tbl_hr` SET `profile_image`='$target' WHERE `hr_usr_id`='$hrid'");

tbl_hr is your database table name, you can create any other name whatever you want.

unlink($delete); function will delete your image from the profile_image folder. Make sure you have to create a folder profile_image in the project directory.

The in_array() function searches an array for a specific value.

Note: If the search parameter is a string and the type parameter is set to TRUE, the search is case-sensitive.

authentication.php

 

session_start();

if(!isset($_SESSION['session_id']) && !isset($_SESSION['hr_usr_id']))

{

    $_SESSION['status']="Please login to access this page";

    header("Location:/");

    exit(0);

}

?>

We are passing hidden input field id with the help of Global variable $_SESSION   on update_profile_image.php file. So we must store specific ID on the server side.

authentication.php

file used if user not authorized it will redirect to home page.

Thanks for reading……

Leave a Comment
No Comments found