Search
 
SCRIPT & CODE EXAMPLE
 

PHP

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

        }

    }
    /*****************************************************************************/
Comment

PREVIOUS NEXT
Code Example
Php :: wordpress show notice 
Php :: php difference between two dates 
Php :: laravel blade dump 
Php :: php mysqli connect err0r 
Php :: increase memory laravel controller 
Php :: acf options repeater 
Php :: mysqli real escape string php 
Php :: laravel sort by numbers 
Php :: how to publish stubs in laravel 
Php :: get all artisan commands 
Php :: interface x trait in php 
Php :: db::statement in laravel 
Php :: how to format php document in vs code 
Php :: php json encode 
Php :: get taxonomies for custom post type 
Php :: laravel routing controller get and post method 
Php :: change returning datetime timezone to recalculate with user timezone laravel 
Php :: sleep php 
Php :: woocommerce get variation price range 
Php :: count remaining days php 
Php :: laravel difference between fill and update 
Php :: laravel model limit 
Php :: laravel redirect back with errors and input 
Php :: get single column value in laravel eloquent 
Php :: date formate in php 
Php :: phpcs 
Php :: laravel 8 – remove public from url 
Php :: php uppercase with accent 
Php :: populate old value of dropdown laravel 
Php :: eloquent get only some columns 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =