In this article, how to create PHP for file upload using PDO.File upload functionality into a temporary directory of the webserver and relocated the target destination to the folder by using PHP. 


Table of contents

 create a database 

 create an HTML upload file form 

 create a PHP file upload script 

 

php for file upload


 

Create a Database 


In the first, we want to create a database to connect for the file path of an image and name.


create table projects(
id int not null auto_increment primary key,
image varchar(50) not null);




   
<?php

    $hostname = "localhost";
    $username = "root";
    $password = "";

    try {
        $conn = new PDO("mysql:host=$hostname;dbname=dailyaspirants", $username, $password);
       $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch(PDOException $e) {
        echo "Database connection failed: " . $e->getMessage();
    }

?>
     
  



create an HTML upload File Form

In the HTML form attribute to mention method and enctype="multipart/form-data" is need required to upload file.
  
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

</head>
<body>
<div class="wrapper">
<div class="container">
	<div class="col-lg-12">
		
		 <?php
if (isset($errorMsg)) {
?>
       <div class="alert alert-danger">
        <strong>check the file. ! <?php echo $errorMsg; ?></strong>
        </div>
         <?php
}
if (isset($insertMsg)) {
?>
	  <div class="alert alert-success">
	<strong>Upload succesfull.! <?php echo $insertMsg; ?></strong>
	</div>
        <?php
}
?>   
		
			<form method="post" class="form-horizontal" enctype="multipart/form-data">
			<div class="form-group mt-5">
				<label class="col-sm-3 control-label">File</label>
				<br>
				<br>
				<div class="col-sm-6">
				<input type="file" name="txt_file" class="form-control" id="chooseFile" />
				</div>
				</div>
			    
					
				<div class="form-group mt-5">
				<div class="col-sm-offset-3 col-sm-9 m-t-15">
				<input type="submit"  name="submit" class="btn btn-success " value="Insert">
				<a href="index.php" class="btn btn-danger">Cancel</a>
				</div>
				</div>
			</form>
			
		</div>
		
	</div>
			
	</div>
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
	
	</body>
</html>  
  
  



create a PHP file upload script

$_FILES IS THE ARRAY can upload files from the client-side is used to find analysis the file, size, and type. 

 ["file"]["name"] - file name

 ["file"]["type"] - file type it is jpg,png,gif.

 ["file"]["size"] - size in bytes

 ["file"]["tmp_name"] - temporary file name 

And target the destination using of move_uploaded_file().

  
 <?php
if(isset($_REQUEST['submit']))
{
	try
	{
			
		$image_file	= $_FILES["txt_file"]["name"];
		$type		= $_FILES["txt_file"]["type"];	
		$size		= $_FILES["txt_file"]["size"];
		$temp		= $_FILES["txt_file"]["tmp_name"];
		
		$path="projects/".$image_file; 
		
		if(empty($image_file)){
			$errorMsg="Please Select Image";
		}
		else if($type=="image/jpg" || $type=='image/jpeg' || $type=='image/png') 
		{	
			if(!file_exists($path)) 
			{
				if($size < 6000000) 
				{
					move_uploaded_file($temp, "projects/" .$image_file); 
				}
				else
				{
					$errorMsg="Your File To large Please Upload 6MB Size"; 
				}
			}
			else
			{	
				$errorMsg="File Already Exists.."; 
			}
		}
		else
		{
			$errorMsg="CHECK FILE EXTENSION JPG,JPEG,PNG File Formats.!"; 
		}
		
		if(!isset($errorMsg))
		{
			$insert_stmt=$conn->prepare('INSERT INTO projects(images) VALUES(:fimage)'); 
			$insert_stmt->bindParam(':fimage',$path);	  
		
			if($insert_stmt->execute())
			{
				$insertMsg="File Upload Successfully.!"; 
				header("refresh:3;index.php"); 
			}
		}
	}
	catch(PDOException $e)
	{
		echo $e->getMessage();
	}
	
}
?>
  
  
php for file upload


Post a Comment

Previous Post Next Post