Lokang 

PHP and MySQL

File

Creating a PHP and MySQL application that includes file upload functionality with CRUD (Create, Read, Update, Delete) operations can be broken down into several steps. Below is a step-by-step guide to help you build this application.

Step 1: Set Up Your Database

Create a MySQL database and a table to store the file information.

CREATE DATABASE file_upload;
USE file_upload;
CREATE TABLE files (
   id INT AUTO_INCREMENT PRIMARY KEY,
   name VARCHAR(255) NOT NULL,
   path VARCHAR(255) NOT NULL,
   uploaded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Step 2: Create a Database Connection

Create a file called db.php to handle the database connection.

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "file_upload";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
   die("Connection failed: " . $conn->connect_error);
}
?>

Step 3: Create the File Upload Form

Create a file called index.php for the file upload form.

<!DOCTYPE html>
<html>
<head>
   <title>File Upload</title>
</head>
<body>
   <h2>Upload File</h2>
   <form action="upload.php" method="post" enctype="multipart/form-data">
       <label for="file">Select file:</label>
       <input type="file" name="file" id="file" required>
       <button type="submit">Upload</button>
   </form>
</body>
</html>

Step 4: Handle File Upload

Create a file called upload.php to handle the file upload.

<?php
include 'db.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['file'])) {
   $fileName = $_FILES['file']['name'];
   $fileTmpName = $_FILES['file']['tmp_name'];
   $uploadDirectory = 'uploads/';
   $filePath = $uploadDirectory . basename($fileName);
   if (move_uploaded_file($fileTmpName, $filePath)) {
       $sql = "INSERT INTO files (name, path) VALUES ('$fileName', '$filePath')";
       if ($conn->query($sql) === TRUE) {
           echo "File uploaded successfully.";
       } else {
           echo "Error: " . $sql . "<br>" . $conn->error;
       }
   } else {
       echo "There was an error uploading your file.";
   }
}
$conn->close();
?>

Step 5: Display Uploaded Files

Create a file called list.php to display the uploaded files.

<?php
include 'db.php';
$sql = "SELECT * FROM files";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html>
<head>
   <title>Uploaded Files</title>
</head>
<body>
   <h2>Uploaded Files</h2>
   <table border="1">
       <tr>
           <th>ID</th>
           <th>Name</th>
           <th>Path</th>
           <th>Uploaded At</th>
           <th>Action</th>
       </tr>
       <?php
       if ($result->num_rows > 0) {
           while($row = $result->fetch_assoc()) {
               echo "<tr>
                   <td>{$row['id']}</td>
                   <td>{$row['name']}</td>
                   <td>{$row['path']}</td>
                   <td>{$row['uploaded_at']}</td>
                   <td>
                       <a href='download.php?id={$row['id']}'>Download</a> |
                       <a href='delete.php?id={$row['id']}'>Delete</a>
                   </td>
               </tr>";
           }
       } else {
           echo "<tr><td colspan='5'>No files found.</td></tr>";
       }
       ?>
   </table>
</body>
</html>
<?php
$conn->close();
?>

Step 6: File Download

Create a file called download.php to handle file downloads.

<?php
include 'db.php';
if (isset($_GET['id'])) {
   $id = $_GET['id'];
   $sql = "SELECT * FROM files WHERE id = $id";
   $result = $conn->query($sql);
   if ($result->num_rows == 1) {
       $row = $result->fetch_assoc();
       $filePath = $row['path'];
       if (file_exists($filePath)) {
           header('Content-Description: File Transfer');
           header('Content-Type: application/octet-stream');
           header('Content-Disposition: attachment; filename="'.basename($filePath).'"');
           header('Expires: 0');
           header('Cache-Control: must-revalidate');
           header('Pragma: public');
           header('Content-Length: ' . filesize($filePath));
           readfile($filePath);
           exit;
       } else {
           echo "File not found.";
       }
   } else {
       echo "Invalid file ID.";
   }
}
$conn->close();
?>

Step 7: File Deletion

Create a file called delete.php to handle file deletion.

<?php
include 'db.php';
if (isset($_GET['id'])) {
   $id = $_GET['id'];
   $sql = "SELECT * FROM files WHERE id = $id";
   $result = $conn->query($sql);
   if ($result->num_rows == 1) {
       $row = $result->fetch_assoc();
       $filePath = $row['path'];
       if (file_exists($filePath)) {
           unlink($filePath);
           $sql = "DELETE FROM files WHERE id = $id";
           if ($conn->query($sql) === TRUE) {
               echo "File deleted successfully.";
           } else {
               echo "Error deleting file: " . $conn->error;
           }
       } else {
           echo "File not found.";
       }
   } else {
       echo "Invalid file ID.";
   }
}
$conn->close();
?>

Step 8: Combine Everything

Ensure all files are in the same directory and accessible. Create an uploads directory with appropriate permissions for file uploads.

This completes a basic CRUD application with file upload functionality in PHP and MySQL. You can extend and customize this further based on your requirements.