How to upload and validate a image in php -PHPGURUKUL.


Returns the processed image in requested outputformat. From this event you can add optional hooks.

  methods: {
    setImage: function (file) {
      this.hasImage = true
      this.image = file


On end of upload.


On start of upload.


Specifies the types of files that can be submitted through the file upload. The types can be valid file extension starting with the STOP character (e.g: «.gif, .jpg, .png») or wildcare file types (e.g. audio/, video/, image/*»). To specify more than one value, separate the values with a comma

  • type: String
  • default: ‘image/*’


Sets the desired class name for the input element

  • type: String or Array
  • default: ‘fileinput’

Compiles and hot-reloads for development

yarn run serve

Compiles and minifies for production

yarn run build

Compiles and minifies lib bundle

yarn run build-lib


How much to write to the console. 0 = silent. 1 = quiet. 2 = loud


Specifies filetypes that will not be resized. Accepts an array of image’s extension. If only 1 extension, it can be provided directly as a string.

  • type String or Array
  • default: null

How to upload and validate a image in php -phpgurukul.

In this tutorial I will explain how to upload a image and how to validate image extension
Create an HTML form with two text field :

  • One is for Image title with input type text.
  • Second one is for upload image with input type file.For file upload input must be file(type=”file”). This input type show the file select control.

How to upload and validate a image in php -PHPGURUKUL.

HTML form must have following attributes :

  • method=”post”
  • enctype=”multipart/form-data” (The enctype attribute specifies how the form-data should be encoded when submitting it to the server.)

without these attributes , the image upload will not work.

<form name="uploadimage"  enctype="multipart/form-data" method="post">
<table width="100%"  border="0">
<th width="26%" height="60" scope="row">Image Title:</th>
<td width="74%"><input type="text" name="imagetitle"  autocomplete="off" class="form-control" required /></td>
<th height="60" scope="row">Upload Image :</th>
<td><input type="file" name="image"  required /></td>
<th height="60" scope="row">&nbsp;</th>
<td><input type="submit" value="Submit" name="submit" class="btn-primary" /></td>

Create a database with name imagesdata. Inside this database we create a table with name tblimages.
SQL Script for tblimages—

  `id` int(11) NOT NULL,
  `ImagesTitle` varchar(120) DEFAULT NULL,
  `Image` varchar(150) DEFAULT NULL,

Now create a database connection file(config.php)

define('DB_PASS' ,'');
define('DB_NAME', 'imagesdata');
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
// Check connection
if (mysqli_connect_errno())
 echo "Failed to connect to MySQL: " . mysqli_connect_error();

PHP Script for getting posted  values,  image validation, move images into directory and  insertion data into database

// Posted Values
// get the image extension
$extension = substr($imgfile,strlen($imgfile)-4,strlen($imgfile));
// allowed extensions
$allowed_extensions = array(".jpg","jpeg",".png",".gif");
// Validation for allowed extensions .in_array() function searches an array for a specific value.
echo "<script>alert('Invalid format. Only jpg / jpeg/ png /gif format allowed');</script>";
//rename the image file
// Code for move image into directory
// Query for insertion data into database
$query=mysqli_query($con,"insert into tblimages(ImagesTitle,Image) values('$imgtitle','$imgnewfile')");
echo "<script>alert('Data inserted successfully');</script>";
echo "<script>alert('Data not inserted');</script>";

How to run this script 

  1. Download the zip
  2. Extract zip file and put in the root directory
  3. open phpmyadmin. Create a db imagesdata then import SQL file(given inside the package)

Input label slot

An optional label tag can be added as a slot

Lints and fixes files

yarn run lint


An integer in pixels for the maximum height allowed for uploaded images, selected images with a greater height than this value will be scaled down before upload.

  • type: Number,
  • default: 1024


An integer in pixels for the maximum width allowed for uploaded images, selected images with a greater width than this value will be scaled down before upload.

  • type: Number
  • default: 1024


A boolean flag to toogle an img-tag displaying the uploaded image. When set to false no img-tag is displayed.

  • type: Boolean,
  • default: true

Project setup

yarn install


A float between 0 and 1.00 for the image quality to use in the resulting image data, around 0.9 is recommended.

  • type: Number,
  • default: 1.00

Roadmap and todos

  1. Progress report
  2. Support multiple files
  3. Implement completion callback
  4. Propper unit testing of events
  5. Clean up scaffolding and project files
  6. Exclude optional dependecies from package

Run your tests

yarn run test


Allows scaling down to a specified fraction of the original size. (Example: a value of 0.5 will reduce the size by half.) Accepts a decimal value between 0 and 1.

  • type: Number,
  • default: null


In script entry point

import ImageUploader from 'vue-image-upload-resize'

Vue image upload and resize

A Vue.js Plugin Component for client-side image upload with optional resizing and exif-based autorotate.

Оставьте комментарий