Multiple File Upload
Submitted by umind514 on Monday, December 12, 2016 - 11:41.
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.).
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
add_file_query.php - Script for Adding Files to Database
file_list.php -- Script for viewing file records
delete_file_query.php -script for deleting a file
SAMPLE CODE
connect.php- Establish connection on localhost- <?php
- $host = "localhost";
- $user = "root";
- $pass = "";
- try {
- $con = new PDO("mysql:host=$host;dbname=mfu", $user, $pass);
- $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- } catch (PDOException $e) {
- echo 'Connection Failed:' . $e->getMessage();
- }
- ?>
- <?php include ("connect/connect.php"); ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Multiple File Upload with download</title>
- <link rel="stylesheet" type="text/css" href="style.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
- </head>
- <body>
- <div class="form">
- <form action="addfile_query.php" method="POST" enctype = "multipart/form-data">
- <label>Upload File</label>
- <input type= "file" name="file" id="file">
- <br>
- <button type="submit" name="addfile">Submit</button>
- <a href="file_list.php">View Records</a>
- </form>
- </div>
- </body>
- </html>
- <?php
- include('connect/connect.php');
- $target_dir = "files/";
- $uploadOk = 1;
- } else {
- echo "Sorry, there was an error uploading your file.";
- }
- $file = $target_file; //
- $addfile = $con->prepare("INSERT INTO file (file_name , file_type) VALUES (?,?)");
- echo "<script>alert('New File Uploaded'); window.location.href = 'index.php'; </script>";
- }
- ?>
- <?php include ("connect/connect.php"); ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Multiple File Upload with download</title>
- <link rel="stylesheet" type="text/css" href="style.css">
- </head>
- <body>
- <div class="table">
- <p>Records</p>
- <table>
- <thead>
- <tr>
- <td>ID</td>
- <td>File Name</td>
- <td>Action</td>
- </tr>
- </thead>
- <tbody>
- <?php
- include ("connect/connect.php");
- $file_list = $con->prepare("SELECT * FROM file");
- $file_list->execute();
- $fetch = $file_list->fetchall();
- foreach ($fetch as $key => $row) {
- ?>
- <td><?php echo $row['file_id'] ?></td>
- <td><?php echo $row['file_name'] ?></td>
- <td><a href="delete_query.php?file_id=<?php echo$row['file_id']; ?>">Delete</a></td>
- </tbody>
- <?php } ?>
- </table>
- </div>
- </body>
- </html>
- <?php
- include('connect/connect.php');
- $file_id = $_GET['file_id'];
- $delete = $con->prepare("DELETE FROM file WHERE file_id = :file_id");
- $delete->bindParam(':file_id', $file_id);
- $delete->execute();
- echo "<script>alert('File Deleted'); window.location.href = 'file_list.php'; </script>";
- ?>
Add new comment
- 1096 views