Answers for "upload image validation in php"

PHP
1

image uploading and validation php

/***********************************************************************************************/
    $image = $_FILES['image']['name'];
    $imageFlag = true;
    if (empty($image)) {
      /*  if($gender == "male")
            $imageName = 'default1.jpeg';
        else
            $imageName = 'default2.png';*/
        $imageFlag = false;
    } else {


        $file_extension = strtolower(pathinfo($_FILES["image"]["name"], PATHINFO_EXTENSION));

        $allowed_image_extension = array(
            "jpeg",
            "png",
            "jpg"
        );
        // Get Image Dimension
        $fileinfo = @getimagesize($_FILES["image"]["tmp_name"]); //$fileinfo var contains an array of info about the image
        $width = $fileinfo[0]; //array at index 0 contains the width of image
        $height = $fileinfo[1];  // array index 1 contains the height of image
        /*check image diamentions*/
        if ($width > 1500 || $height > 1600 || $width > $height) {
            $output['error']['image'] = "* image dimensions should be less than 400*500 and width should be less than height i.e passport size image";
            $imageFlag = false;

        }

        /*check image extension*/
        if (!in_array($file_extension, $allowed_image_extension)) {
            $imageFlag = false;
            $output['error']['image'] = "Only jpg , jpeg and png format are allowed";
        }
        /*Check image size*/
        if (($_FILES["image"]["size"] > 20000000)) {
            $imageFlag = false;
            $output['error']['image']= "Image size should be less than 2mb";
        }
        if ($imageFlag) {
            /*or uniqueid*/
            $imageName = time() . 'Teacher.' . $file_extension;
            $target = ("images/teacher/" . basename($imageName));
            $m = move_uploaded_file($_FILES['image']['tmp_name'], $target);/*moves the image into the server*/

        }

    }
    /*****************************************************************************/
Posted by: Guest on May-19-2021

Code answers related to "upload image validation in php"

Browse Popular Code Answers by Language