Skip to content
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
File upload and validation in php

File upload and validation in php

Posted on June 12, 2018June 9, 2020 By XpertPhp 1 Comment on File upload and validation in php

In this post, we are going to show you how to File upload and validation in php. At first, we will create an HTML form in this form tag in pass an enctype=”multipart/form-data”. It specifies which content-type to use when submitting the form.

Create images folder on your project directory, when submitting a form at that time check validation, if that time validation true then an image will be inserted in the database and image will be uploaded in the images folder otherwise if any error then shows the error message.

See Below Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
if(isset($_POST['btnadd']))
{
if(isset($_FILES))
{
$file_name = $_FILES['imgFile']['name'];
$file_size = $_FILES['imgFile']['size'];
$file_tmp =  $_FILES['imgFile']['tmp_name'];
$file_type=  $_FILES['imgFile']['type'];
$acceptable = array('image/jpeg','image/jpg','image/png');
 
if(!in_array($file_type, $acceptable))
{
     $errors[]= "extension not allowed, please choose a JPEG or PNG file.";
}
else
{
include('connection.php');
$insert="insert into register (image) values('$file_name')";
mysql_query($conn,$insert);
move_uploaded_file($file_tmp,"images/".$file_name);
header('location:index.php?msg=Data Successfull Inserted');
}
}
}
if(!empty($errors))
{
   echo '<script>alert("'.$errors[0].'");</script>';  
}
?>
 
<form method="post" enctype="multipart/form-data">
<table align="center">
<tr>
   <td colspan="2" align="center">Add Image</td>
</tr>
<tr>
   <td>Image</td>
   <td><input type="file" name="imgFile"> </td>
</tr>
<tr>
   <td colspan="2" align="center"><input type="submit" value="Add" name="btnadd"> </td>
</tr>
</table>
</form>

Please follow and like us:
error
fb-share-icon
Tweet
fb-share-icon

Recommended Posts:

  • Laravel 7 Fullcalendar Example Tutorial
  • Laravel one to many eloquent relationship tutorial example
  • laravel eloquent sort query example
  • Star Rating System Using PHP and Jquery with ajax
  • CodeIgniter 4 Rest Api Example Tutorial
MySql, Php Tags:file upload, File Validation, Image Upload, php with mysql

Post navigation

Previous Post: Login logout and register example in Codeigniter
Next Post: how to Integrate Razorpay payment gateway in Codeigniter

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tricks jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch Laravel Socialite laravel social login nodejs pagination payment gateway php with mysql react js tutorial rewrite rule send mail validation wysiwyg editor

Latest Posts

  • How to Convert Date and Time from one timezone to another in php
  • how to get current date and time in php
  • Drag and Drop Reorder Items with jQuery, PHP & MySQL
  • Laravel 9 Toastr Notifications Example Tutorial
  • Laravel 9 CRUD Operation Example Using Google Firebase
  • Laravel 9 CKeditor Image Upload With Example
  • Laravel 9 Summernote Image Upload With Example
  • Laravel 9 Stripe Payment Gateway Integrate Example
  • How To Send Email Using Mailtrap In Laravel 9
  • Laravel 9 Fullcalendar Ajax Example Tutorial

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Age Calculator Online
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Copyright © 2018 - 2022,

All Rights Reserved Powered by XpertPhp.com