├── README.md
├── downloads.php
├── file-management.sql
├── filesLogic.php
├── index.php
├── style.css
└── uploads
└── digital-ad.zip
/README.md:
--------------------------------------------------------------------------------
1 | # PHP-MySQL-File-upload
2 | How to upload and download files PHP and MySQL
3 |
4 | > This code example demonstrates the process to implement the file upload functionality in web applications and the following functionality will be implemented.
5 |
6 | - [x] HTML form to upload .zip, .pdf, .docx, .ppt, as well as image files.
7 | - [x] Upload .zip, .pdf, .docx, .ppt, as well as image files to server using PHP.
8 | - [x] Store file name in the database using PHP and MySQL.
9 | - [x] Retrieve .zip, .pdf, .docx, .ppt, as well as image files from the database and display in the web page.
10 |
--------------------------------------------------------------------------------
/downloads.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Download files
8 |
9 |
10 |
11 |
12 |
13 | ID
14 | Filename
15 | size (in mb)
16 | Downloads
17 | Action
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 | Download
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/file-management.sql:
--------------------------------------------------------------------------------
1 | -- phpMyAdmin SQL Dump
2 | -- version 4.9.0.1
3 | -- https://www.phpmyadmin.net/
4 | --
5 | -- Host: 127.0.0.1
6 | -- Generation Time: Jul 22, 2019 at 09:18 AM
7 | -- Server version: 10.3.15-MariaDB
8 | -- PHP Version: 7.3.6
9 |
10 | SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
11 | SET AUTOCOMMIT = 0;
12 | START TRANSACTION;
13 | SET time_zone = "+00:00";
14 |
15 |
16 | /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
17 | /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
18 | /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
19 | /*!40101 SET NAMES utf8mb4 */;
20 |
21 | --
22 | -- Database: `file-management`
23 | --
24 |
25 | -- --------------------------------------------------------
26 |
27 | --
28 | -- Table structure for table `files`
29 | --
30 |
31 | CREATE TABLE `files` (
32 | `id` int(11) NOT NULL,
33 | `name` varchar(255) NOT NULL,
34 | `size` int(11) NOT NULL,
35 | `downloads` int(11) NOT NULL
36 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
37 |
38 | --
39 | -- Dumping data for table `files`
40 | --
41 |
42 | INSERT INTO `files` (`id`, `name`, `size`, `downloads`) VALUES
43 | (2, 'digital-ad.zip', 56349, 1);
44 |
45 | --
46 | -- Indexes for dumped tables
47 | --
48 |
49 | --
50 | -- Indexes for table `files`
51 | --
52 | ALTER TABLE `files`
53 | ADD PRIMARY KEY (`id`);
54 |
55 | --
56 | -- AUTO_INCREMENT for dumped tables
57 | --
58 |
59 | --
60 | -- AUTO_INCREMENT for table `files`
61 | --
62 | ALTER TABLE `files`
63 | MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
64 | COMMIT;
65 |
66 | /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
67 | /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
68 | /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
69 |
--------------------------------------------------------------------------------
/filesLogic.php:
--------------------------------------------------------------------------------
1 | 1000000) { // file shouldn't be larger than 1Megabyte
28 | echo "File too large!";
29 | } else {
30 | // move the uploaded (temporary) file to the specified destination
31 | if (move_uploaded_file($file, $destination)) {
32 | $sql = "INSERT INTO files (name, size, downloads) VALUES ('$filename', $size, 0)";
33 | if (mysqli_query($conn, $sql)) {
34 | echo "File uploaded successfully";
35 | }
36 | } else {
37 | echo "Failed to upload file.";
38 | }
39 | }
40 | }
41 |
42 | // Downloads files
43 | if (isset($_GET['file_id'])) {
44 | $id = $_GET['file_id'];
45 |
46 | // fetch file to download from database
47 | $sql = "SELECT * FROM files WHERE id=$id";
48 | $result = mysqli_query($conn, $sql);
49 |
50 | $file = mysqli_fetch_assoc($result);
51 | $filepath = 'uploads/' . $file['name'];
52 |
53 | if (file_exists($filepath)) {
54 | header('Content-Description: File Transfer');
55 | header('Content-Type: application/octet-stream');
56 | header('Content-Disposition: attachment; filename=' . basename($filepath));
57 | header('Expires: 0');
58 | header('Cache-Control: must-revalidate');
59 | header('Pragma: public');
60 | header('Content-Length: ' . filesize('uploads/' . $file['name']));
61 |
62 | //This part of code prevents files from being corrupted after download
63 | ob_clean();
64 | flush();
65 |
66 | readfile('uploads/' . $file['name']);
67 |
68 | // Now update downloads count
69 | $newCount = $file['downloads'] + 1;
70 | $updateQuery = "UPDATE files SET downloads=$newCount WHERE id=$id";
71 | mysqli_query($conn, $updateQuery);
72 | exit;
73 | }
74 |
75 | }
76 |
--------------------------------------------------------------------------------
/index.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Files Upload and Download
7 |
8 |
9 |
18 |
19 |
--------------------------------------------------------------------------------
/style.css:
--------------------------------------------------------------------------------
1 | form {
2 | width: 30%;
3 | margin: 100px auto;
4 | padding: 30px;
5 | border: 1px solid #555;
6 | }
7 | input {
8 | width: 100%;
9 | border: 1px solid #f1e1e1;
10 | display: block;
11 | padding: 5px 10px;
12 | }
13 | button {
14 | border: none;
15 | padding: 10px;
16 | border-radius: 5px;
17 | }
18 | table {
19 | width: 60%;
20 | border-collapse: collapse;
21 | margin: 100px auto;
22 | }
23 | th,
24 | td {
25 | height: 50px;
26 | vertical-align: center;
27 | border: 1px solid black;
28 | }
--------------------------------------------------------------------------------
/uploads/digital-ad.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MarkxOBrian/PHP-MySQL-File-upload/c0bf141a7bf5ce2a7ead2e4a48a0ddf3ef66caf5/uploads/digital-ad.zip
--------------------------------------------------------------------------------