FILE DOWNLOAD AND UPLOAD SYSTEM USING PHP AND MYSQL:
Mục lục bài viết
FILE DOWNLOAD AND UPLOAD SYSTEM USING PHP AND MYSQL:
- (code link below)
Before we start Read a short article that might interest you.
This will be a short and comprehensive “how-to” on a system built to upload files irrespective of the extension. The file contents will be saved on to a table with some attributes to identify each of the files.
Pre-requisite knowledge required for following along
- Basic HTML
- PHP
- SQL/SQL lite
This will be a fairly simple interface with a PHP command with context stored in the BLOB.
FILES REQUIRED
- Upload — this will be used to upload the files into webserver with attributes to identify all the file structures.
- Index — this will be used to take input of the attribute with which we are gonna identify the file.
- Download — this will the result of a search query.
- Download2 — this will be the last page that will actually force out the content.
SQL TABLES
The rows consist of an id that consists of integer which auto-increments its value on the addition of more files.
The content is stored in BLOB data type, the rest of the attributes such as names and phone numbers are added in order to recognize which are just added to recognize the file which can be taken as input.
The other rows are the name of the file and extension to see what kind of file is it.
UPLOAD
To upload a file we are gonna create a form and use it to take input of a file and then two text input fields of the name and phone number. Then redirect the action to a function that will validate the inputs and run a query to save it into the table with name as file name and extension.
INDEX
The index file will contain a form that can be called to enter an attribute that will be available to anyone who wants to download.
DOWNLOAD
This file will be the redirect endpoint of the index and will fetch any number of rows which has the attributes in my case phone number which can be download on click.
Download2
This is the file where another query to select the file by its id will be made and finally, the headers will be set to force download the file which has been stored in the content.
The header should be set carefully and the browser permission should for the force download to work.
header("Content-length: $size");
header("Content-type: $type");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=$name");
header("Content-Type: application/octet-stream;");
LINK TO THE CODE