In this tutorial, you’ll learn how to upload a file to a remote web server with PHP. You will learn how to upload files to a remote server using a Simple HTML form and some PHP. You can upload any kind of files (i.e. images, videos, ZIP files, Microsoft Office documents, PDFs, as well as executables files and a wide range of other file types). We’ll be using material design to display it nice and clean.
Upload files to DB
Step 1: Set up Database
Run the following SQL code
1 2 3 4 5 6 | CREATE TABLE `tbl_uploads` ( `id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY , `file` VARCHAR( 100 ) NOT NULL , `type` VARCHAR( 10 ) NOT NULL , `size` INT NOT NULL ) ENGINE = MYISAM ; |
dbconfig.php
1 2 3 4 5 6 | $dbhost = "localhost"; $dbuser = "YOUR_DB_USERNAME"; $dbpass = "YOUR_DB_PASS"; $dbname = "YOUR_DBNAME"; mysql_connect($dbhost,$dbuser,$dbpass) OR die('Server connection failure!'); mysql_select_db($dbname) OR die('Database selection failure!'); |
Configure front-end upload page
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | <?php include_once 'dbconfig.php'; ?> <!DOCTYPE html> <html xmlns="//www.w3.org/1999/xhtml"> <head> <link rel="stylesheet" href="//fonts.googleapis.com/icon?family=Material+Icons"> <link rel="stylesheet" href="//code.getmdl.io/1.3.0/material.indigo-pink.min.css"> <script defer src="//code.getmdl.io/1.3.0/material.min.js"></script> <title>Upload files</title> </head> <body> <div id="header"> <h3>Upload files</h3> </div> <div id="body"> <form action="upload.php" method="post" enctype="multipart/form-data"> <input class="mdl-textfield__input" type="file" name="file" style="width: 12%"> <button class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--colored" type="submit" name="btn-upload" style="width: 12%;">Upload</button> </form> <br> <?php if(isset($_GET['success'])) { ?> <h4>File Uploaded Successfully...<br><a href="view.php">Click here to view file.</a></h4> <?php } else if(isset($_GET['fail'])) { ?> <h4>Problem with Uploading!</h4> <?php } else { ?> <label>Upload files (PDF, DOC, EXE, VIDEO, MP3, ZIP, etc...)</label> <?php } ?> </div> <div id="footer"> <label>By <a href="//michaelm.altervista.org/">Michael Mavashev</a></label> </div> </body> </html> |
The data is submitted to upload.php which we’re about to set up
Set up receiving PHP code
upload.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php if(isset($_POST['btn-upload'])) { $file = rand(1000,100000)."-".$_FILES['file']['name']; $file_loc = $_FILES['file']['tmp_name']; $file_size = $_FILES['file']['size']; $file_type = $_FILES['file']['type']; $folder="uploads/"; move_uploaded_file($file_loc,$folder.$file); $sql="INSERT INTO tbl_uploads(file,type,size) VALUES('$file','$file_type','$file_size')"; mysql_query($sql); } ?> |
Display files from DB
The page “view.php” will allow users to see the uploaded files
view.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | <?php include_once 'dbconfig.php'; ?> <!DOCTYPE html> <html xmlns="//www.w3.org/1999/xhtml"> <head> <link rel="stylesheet" href="//fonts.googleapis.com/icon?family=Material+Icons"> <link rel="stylesheet" href="//code.getmdl.io/1.3.0/material.indigo-pink.min.css"> <script defer src="//code.getmdl.io/1.3.0/material.min.js"></script> <title>File upload</title> </head> <body> <div id="header"> <h3>File upload</h3> </div> <div id="body"> <h5 style="padding-left: 7%;">Your uploads... <a href="index.php">Upload new files...</a></h5> <table class="mdl-data-table mdl-js-data-table mdl-data-table--selectable mdl-shadow--2dp" width="25%" border="1"> <thead> <tr> <th class="mdl-data-table__cell--non-numeric">File Name</th> <th>File Type</th> <th>File Size(KB)</th> <th>View</th> </tr> </thead> <?php $sql="SELECT * FROM tbl_uploads"; $result_set=mysql_query($sql); while($row=mysql_fetch_array($result_set)) { ?> <tr> <td><?php echo $row['file'] ?></td> <td><?php echo $row['type'] ?></td> <td><?php echo $row['size'] ?></td> <td><a href="uploads/<?php echo $row['file'] ?>" target="_blank">View File</a></td> </tr> <?php } ?> </table> </div> </body> </html> |
That’s all it takes! Enjoy using my script 🙂