Skip to content

ThirteeNov

My personal blog about coding and internet

Menu
  • About me
  • About Zofia Kreasi
  • Cart
  • Checkout
  • Making an airplane game from scratch in Unity
  • My account
  • Privacy Policy
  • Privacy Policy – zkLeaderboard
  • Sample Page
  • Shop
  • Tutorials on Learning JavaScript
  • ZKAccounts – Privacy Policy
Menu

Checking uploaded file extension with PHP before storing it on our server

Posted on November 10, 2019August 4, 2020 by Habibie

We often allow our users to upload files to our server. It is important to limit file extensions that we accept.

For example, we need to only accept .pdf and .txt files from our users, we don’t want them to send us any other files instead of pdf and txt. Then this snippet is useful to check their file extension before accepting it:

<!DOCTYPE html>
<html>
	<head>
		<title>File Extension Check</title>
	</head>
	<body>
		<?php 
		if(isset($_POST["extcheck"])){
			$acceptedext = array("pdf", "txt");
			$uploadedfile = $_FILES["myfile"]["name"];
			$extension = pathinfo($uploadedfile, PATHINFO_EXTENSION);
			if(!in_array($extension, $acceptedext)){
				echo "The file is not accepted.";
			}else{
				echo "The file is uploaded.";
			}
		}else{
			?>
			<form method="post" enctype="multipart/form-data">
				<input type="file" name="myfile" accept=".pdf"><br>
				<input type="submit" value="Submit" name="extcheck">
			</form>
			<?php
		}
		?>
	</body>
</html>

Watch the video here:

Post Views: 411
ciihuy2020

Welcome!

  • My YouTube Channel
  • My GitHub Page
  • About me

Categories

  • 3DVista
  • Android
  • Apache
  • C#
  • Cordova
  • Electron & Node JS
  • HTML5, CSS & JavaScript
  • iOS
  • Let's Make Unity Games
  • Misc
  • Photoshop
  • PHP
  • Python
  • Uncategorized
  • Unity
  • WordPress

Recent Posts

  • Hover Reveal script for Unity to show and hide object on mouse hover
  • How to Prevent UI Clicks in Unity from “Bleeding Through” to 3D Objects Behind Them
  • Make objects like wires and cables easily in Unity using Ciihuy Curved Mesh
  • [SOLVED] Can’t Add Custom Domain to Blogger After Losing CNAME Verification
  • iOS App Icon Generator by CiihuyCom
© 2026 ThirteeNov | Powered by Superbs Personal Blog theme