Multiple File Upload

Hello, In this tutorial we will create a simple multiple file upload using PHP, MySQL-PDO. This application allows the user to upload all types of file (Images, Docs, PDF and etc.).

SAMPLE CODE

connect.php- Establish connection on localhost
  1. <?php
  2.         $host = "localhost";
  3.         $user = "root";
  4.         $pass = "";
  5.  
  6.         try {
  7.                 $con = new PDO("mysql:host=$host;dbname=mfu", $user, $pass);
  8.                 $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  9.         } catch (PDOException $e) {
  10.                 echo 'Connection Failed:' . $e->getMessage();
  11.         }
  12. ?>
Index.php- Script for Main Page *note* enctype = "multipart/form-data" must be included into form tag or else the file wo'nt be sent through the POST
  1. <?php include ("connect/connect.php"); ?>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5.         <title>Multiple File Upload with download</title>
  6.         <link rel="stylesheet" type="text/css" href="style.css">
  7.            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  8. </head>
  9. <body>
  10. <div class="form">
  11.         <form action="addfile_query.php" method="POST" enctype = "multipart/form-data">
  12.         <label>Upload File</label>
  13.         <input type= "file" name="file" id="file">
  14.         <br>
  15.         <button type="submit" name="addfile">Submit</button>
  16.  
  17.         <a href="file_list.php">View Records</a>
  18. </form>
  19. </div>
  20. </body>
  21. </html>
add_file_query.php - Script for Adding Files to Database
  1. <?php
  2. include('connect/connect.php');
  3. if(isset($_POST['addfile'])) {
  4.  
  5. $target_dir = "files/";
  6.     $target_file = $target_dir. basename($_FILES["file"]["name"]);
  7.     $uploadOk = 1;
  8.     $FileType = pathinfo($target_file,PATHINFO_EXTENSION);
  9.  
  10.     if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
  11.     } else {
  12.         echo "Sorry, there was an error uploading your file.";
  13.     }
  14.  
  15.     $file = $target_file; //
  16.  
  17.     $addfile = $con->prepare("INSERT INTO file (file_name , file_type) VALUES (?,?)");
  18.  
  19.     $addfile->execute(array($file , $FileType));
  20.  
  21.         echo "<script>alert('New File Uploaded'); window.location.href = 'index.php'; </script>";
  22.  
  23. }
  24. ?>
file_list.php -- Script for viewing file records
  1. <?php include ("connect/connect.php"); ?>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5.         <title>Multiple File Upload with download</title>
  6.         <link rel="stylesheet" type="text/css" href="style.css">
  7. </head>
  8. <body>
  9.  
  10. <div class="table">
  11. <p>Records</p>
  12.         <table>
  13.                 <thead>
  14.                         <tr>
  15.                                 <td>ID</td>
  16.                                 <td>File Name</td>
  17.                                 <td>Action</td>
  18.                         </tr>
  19.                 </thead>
  20.                 <tbody>
  21.                 <?php
  22.                 include ("connect/connect.php");
  23.                         $file_list = $con->prepare("SELECT * FROM file");
  24.                 $file_list->execute();
  25.             $fetch = $file_list->fetchall();
  26.  
  27.              foreach ($fetch as $key => $row) {
  28.                 ?>
  29.                         <td><?php echo $row['file_id'] ?></td>
  30.                         <td><?php echo $row['file_name'] ?></td>
  31.                         <td><a href="delete_query.php?file_id=<?php echo$row['file_id']; ?>">Delete</a></td>
  32.                 </tbody>
  33.                 <?php } ?>
  34.         </table>
  35. </div>
  36. </body>
  37. </html>
delete_file_query.php -script for deleting a file
  1. <?php
  2. include('connect/connect.php');
  3.  
  4. $file_id = $_GET['file_id'];
  5.         $delete = $con->prepare("DELETE FROM file WHERE file_id = :file_id");
  6.         $delete->bindParam(':file_id', $file_id);
  7.         $delete->execute();
  8.  
  9.                 echo "<script>alert('File Deleted'); window.location.href = 'file_list.php'; </script>";
  10. ?>

Add new comment