PHP Code To Do An HTML File Upload

Here’s an example PHP code that can handle an HTML file upload form:

if(isset($_FILES['uploaded_file'])) {
  $errors = array();
  $file_name = $_FILES['uploaded_file']['name'];
  $file_size = $_FILES['uploaded_file']['size'];
  $file_tmp = $_FILES['uploaded_file']['tmp_name'];
  $file_type = $_FILES['uploaded_file']['type'];
  $file_ext = strtolower(end(explode('.',$_FILES['uploaded_file']['name'])));
  $extensions = array("jpeg","jpg","png");
  if(in_array($file_ext,$extensions)=== false){
     $errors[]="extension not allowed, please choose a JPEG or PNG file.";
  if($file_size > 2097152) {
     $errors[]='File size must be less than 2 MB';
  if(empty($errors)==true) {
     echo "Success";


<form action="" method="POST" enctype="multipart/form-data">
  <input type="file" name="uploaded_file" />
  <input type="submit"/>


This code checks the file extension and size to make sure it meets the required specifications before uploading it to the server. If there are any errors, it prints them to the screen. Otherwise, it moves the file to the “uploads” folder and displays “Success”. Note that you’ll need to create the “uploads” folder and ensure that it has the correct permissions set.

Leave a Reply

Proudly powered by WordPress | Theme: Code Blog by Crimson Themes.