Hello Amigos! You will learn how to upload files in plain PHP in this article.

This article covers

  1. Database Design To Store Data & File Upload Data
  2. HTML For File Upload
  3. DB Connection PHP Code
  4. Getting File Upload & Other Fields Inserting Into Database Working
  5. Simple Validating For Form Fields
  6. Simple Validation For File Uploads

If you have read my other article on How To Upload Image In PHP?  then this one is also similar to that with few modifications in validating the file instead instead of image.

_I have hosted this in GitHub. Feel free to you use is any of your project _Invoice File Upload Demo


Prerequisites

Basic PHP, Database & HTML knowledge.

I have written another article proper way to validate the file in PHP Proper Way To Validate MIME Type Of Files While Handling File Uploads In PHP. Don’t miss it.


1) Database Design To Store Data & File Upload Data

First let me clear you few concepts, we can store the files/images in database as blob but we wont be doing that.

We will store the files/images in specific path of project. Why?

  1. By storing files/images in database it will impact the database speed.
  2. It will soon bloat the size of database which requires larger space and may impact other serious problems

Following is the database design for our testing. As you see its very simple design. For the sake of example we have taken **invoice** as database name.

/** Creating database with name invoice */
CREATE DATABASE `invoice`;

/** Once we create we want to switch to invoice so that we can add products table */
USE `invoice`;

/** Products table to hold the products data */
CREATE TABLE `products` (
	`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
	`name` VARCHAR(30),
	`product_invoice` VARCHAR(255),
	PRIMARY KEY (`id`)
);

NOTE: For the sake of demonstration I am just using Product Invoice as the purchase invoice. It doesn’t make any sense but at the end you will be clear with

For the sake of simplicity lets the file upload only take image, pdf, xls


2) HTML For File Upload

Since we have only 2 fields other than id in our database table products we will have only 2 fields in HTML form.

I wont be using bootstrap or anything for this it will be plain HTML code.

Following is my folder structure

Invoice Project Folder Structure

#php #html

How To Uploads Files In PHP?
1.25 GEEK