File upload and validation in php – onlinecode

File upload and validation in php – onlinecode

In this post we will give you information about File upload and validation in php – onlinecode. Hear we will give you detail about File upload and validation in php – onlinecodeAnd how to use it also give you demo for it if it is necessary.

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

<?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:

Hope this code and post will helped you for implement File upload and validation in php – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

2  +    =  9

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US