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*/

        }

    }
    /*****************************************************************************/

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source