Tagged: How to Upload Files on Server in PHP How to Upload Files on Server in PHP

PHP Simple Image Upload and Display Source Code 0

PHP Simple Image Upload and Display Source Code

PHP Simple Image Upload and Display Source Code

In this Post We Will Explain About is PHP Simple Image Upload and Display Source Code With Example and Demo.Welcome on pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to PHP code for image upload and display Example

In this post we will show you Best way to implement How to Upload Files on Server in PHP, hear for ,
with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

In this post we will make a HMTL to Simple Image Upload using script of the PHP. PHP is a web based server-side PHP scripting language designed mainly primarily for ss side web development. this is a learn and consistent directory way to access mysql databases. This stand developers can script write portable source code more easier. It is best used by a newly freshar coders for its any user or developers user friendly environment. therefor Let’s do the source coding.

making the database dbconection

first of all the Open your mysql any kind of text simple editor( Like as a notepadd++, etc..). and Then just simple copy and paste the source code below after that name it dbcon.php.

<?php
	$dbcon = new mysqli('hostname', 'root', 'password', 'db_pic_name');
 
	if(!$dbcon){
		die("Error!: sorry Cannot dbconect to the database!");
	}
?>

Making The Interface

This is where I will make a simple form for our application. To make the HTML forms simply source code copy and write it into you text notepad editor, and then save data it as shown below source code.

index.php

<!DOCTYPE html>
<html lang="en">
	<head>
		<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
		<meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
	</head>
<body>
	<nav class="navbar navbar-default">
		<div class="container-fluid">
			<a class="navbar-brand" href="https://pakainfo.com">pakainfo.com</a>
	</div>
	</nav>
	<div class="col-sm-3"></div>
	<div class="col-sm-6 well">
		<h3 class="text-primary">Simple PHP - Simple Image Upload - pakainfo</h3>
		<hr style="border-top:1px dotted #ccc;"/>
		<div class="col-sm-2"></div>
		<div>
			<form method="POST" action="live_save_upload.php" enctype="multipart/form-data">
				<label>Name:</label>
				<input type="text" name="name"/>
				<div style="float:right;">
					<div id = "picture_prev_display" style = "width:150px; height :150px; border:1px solid #000;">
						<center id = "myimg">[Photo]</center>
					</div>
					<input type = "file" id = "mypicImg" name = "mypicImg" />
				</div>	
				<div style="clear:both;"></div>
				<br/>
				<center><button class="btn btn-primary" name="submit"><span class="glyphicon glyphicon-save"></span> Save</button></center>
			</form>
		</div>
		<br />
		<div class="col-sm-12">
			<table class="pakainfo table table-bordered exlive">
				<thead>
					<tr>
						<th>User Name</th>
						<th>Photo</th>
					</tr>
				</thead>
				<tbody>
					<?php 
						require 'dbcon.php';
						$squry = $dbcon->query("SELECT * FROM `students`") or die(mysqli_errno());
						while($retrive = $squry->retrive_array()){
 
 
					?>
					<tr>
						<td><?php echo $retrive['name']?></td>
						<td><center><img src="<?php echo "upload/".$retrive['mypicImg']?>" width="100px" height="100px"></center></td>
					</tr>
					<?php
						}
					?>
				</tbody>
			</table>
		</div>
	</div>
</body>
<script src="pakainfo/js/jquery-3.2.1.min.js"></script>
<script type = "text/javascript">
	$(document).ready(function(){
		$pic = $('<img id = "image" width = "100%" height = "100%"/>');
		$myimg = $('<center id = "myimg">[Photo]</center>');
		$("#mypicImg").change(function(){
			$("#myimg").remove();
			var files = !!this.files ? this.files : [];
			if(!files.length || !window.FileReader){
				$("#image").remove();
				$myimg.appendTo("#picture_prev_display");
			}
			if(/^image/.test(files[0].type)){
				var dreadr = new FileReader();
				dreadr.readAsDataURL(files[0]);
				dreadr.onloadend = function(){
					$pic.appendTo("#picture_prev_display");
					$("#image").attr("src", this.result);
				}
			}
		});
	});
</script>
</html>

Creating the Main Function

This source code data contains the specific source code for the upload. This will data process the upload image file that have been upload then will save it to a designated folder. To do that Copy and paste these source code block of source codes inside the main Text editor Like as notepad++ and call it as live_save_upload.php.

<?php
	require_once 'dbcon.php';
 
	if(ISSET($_POST['submit'])){
 
		if($_FILES['mypicImg']['name'] != "" && $_POST['name'] != ""){
			$name = $_POST['name'];
			$pic_name = $_FILES['mypicImg']['name'];
			$pic_temp = $_FILES['mypicImg']['tmp_name'];
			$pic_extention = explode('.', $pic_name);
			$image = time().".".end($pic_extention);
			move_uploaded_file($pic_temp, "upload/".$image);
			$dbcon->query("INSERT INTO `students` VALUES('', '$name', '$image')") or die(mysql_errno());
			header('location:index.php');
		}
	}
?>

so you have it I successfully make a PHP source code example Simple Image Upload using HTML and PHP. I hope that this simple Post help you to what you are looking for. For more many example updates and post and services just kindly all the example and tags visit this site. Good Luck Enjoy Coding!!!

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example

Example

I hope you have Got What is PHP code for image upload and display And how it works.I would Like to have FeedBack From My Blog(pakainfo.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(pakainfo.com) Are Most Always Welcome.