php file upload

      Comments Off on php file upload

php file upload

php file upload

php file upload is common requirement for all php developers and students. Here I have prepared sample tutorial on php file upload which explains how you can upload file into server along with condition like. Limiting size of uploading file and only few type of file you can upload to server.  If you wish to enter filename in to database then you need to refer this article.

This php file upload tutorial will accept file only if:

  • File having size less then 2 MB
  • File having extension like jpg / jpeg only

 

File: index.php

<?php session_start(); 
//php file upload ?>

<html>
	<head>
		<title>	php file upload</title>
	</head>
	<body>
	<center><h2>	php file upload</h2></center>
	
	<form action="index2.php" method="POST" enctype="multipart/form-data">

	<br><br>
	<b>Upload File Attachment <br>(JPEG/JPG Files Only) <br><br></b>
	
	<input type="file" name="logfile" id="logfile"  placeholder="" required/>
	
	<input type="submit" name="submit" value="submit" />
    
  	<?php
	if(isset($_SESSION["msg"]))
	{
		echo "<script language=\"JavaScript\">alert(\"".$_SESSION["msg"]."\");</script>";
		$_SESSION["msg"]="";
		unset($_SESSION["msg"]);
	}
	?>	

	</form>
	</body>
</html>


 

 

File: index2.php

<?php	session_start();
	//php file upload with limitation like only jpg or jpeg file are allowed and max size can be 2 mb
	
	$OneMBSize = 1048576; // 1 MB
	$maxSize = $OneMBSize * 2; // 2 MB 
	if(isset($_POST['submit']))
	{
		//Common Code start
		$filename = $_FILES['logfile']['name'];
		$fileSize = $_FILES['logfile']['size'];
		$extension = explode(".", $filename);
		$total = count($extension);
		if($total > 1)
		{
			$total = $total - 1;
		}
		$fileType = $extension[$total];

		//=============================================================
		// validating file : only jpeg or jpg alloed and size should be less than 2 MB
		//=============================================================
		if(($fileType == "jpeg" || $fileType == "jpg" ) && $fileSize <= $maxSize)
		{
		
			//========================================================================
			//	This is new name of file
			// 	If you wish to save file as custom name then modify this variable value
			//========================================================================
			$mynewname = $_FILES["logfile"]["name"];

			//=========================================================================
			//This line will uplaod your file to upload folder which you have to create first on server and make sure you have write permission else your code may not work
			//=========================================================================
			move_uploaded_file($_FILES["logfile"]["tmp_name"], "upload/$mynewname");

			//====================================
			//Redirect user back to previous page with message
			//===================================
			$_SESSION['msg'] = "File Uploaded";
			header('Location: index.php');
		}
		else
		{
		
			$_SESSION['msg'] = "Invalid file format or file size";
			header('Location: index.php');
		}
	}		
	
?>

 

Uploading valid File using php : Screenshot 1

php file upload

Uploading valid File using php : Screenshot 2

php file upload

Uploading invalid File using php : Screenshot 1

php file upload

Uploading invalid File using php : Screenshot 2

php file upload