Home  >  Article  >  Backend Development  >  How to use PHP to develop simple file management functions

How to use PHP to develop simple file management functions

WBOY
WBOYOriginal
2023-09-20 13:09:271360browse

How to use PHP to develop simple file management functions

How to use PHP to develop simple file management functions

Introduction:
File management functions are an essential part of many Web applications. It allows users to upload, download, delete and display files, providing users with a convenient way to manage files. This article will introduce how to use PHP to develop a simple file management function and provide specific code examples.

1. Create a project
First, we need to create a basic PHP project. Create the following file in the project directory:

  1. index.php: Main page, used to display the upload form and file list.
  2. upload.php: Used to handle file uploads.
  3. delete.php: Used to handle file deletion.

2. Create a database
In order to store file information, we need to create a database. Execute the following SQL statement in MySQL to create a database named "filemanager" and create a table named "files" to store file information:

CREATE DATABASE filemanager;
USE filemanager;

CREATE TABLE files (
  id INT(11) AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  size INT(11) NOT NULL,
  type VARCHAR(255) NOT NULL
);

3. Write code

  1. index.php

    <!DOCTYPE html>
    <html>
    <head>
      <title>文件管理</title>
    </head>
    <body>
      <h1>文件管理</h1>
    
      <h2>上传文件</h2>
      <form action="upload.php" method="post" enctype="multipart/form-data">
     <input type="file" name="file">
     <input type="submit" value="上传">
      </form>
    
      <h2>文件列表</h2>
      <?php
     $conn = new mysqli("localhost", "root", "", "filemanager");
     $result = $conn->query("SELECT * FROM files");
    
     if ($result->num_rows > 0) {
       while ($row = $result->fetch_assoc()) {
         echo "<a href='download.php?id=" . $row["id"] . "'>" . $row["name"] . "</a> (" . $row["size"] . " bytes)<br>";
       }
     } else {
       echo "暂无文件";
     }
    
     $conn->close();
      ?>
    </body>
    </html>
  2. upload.php

    <?php
      $targetDirectory = "uploads/";
    
      $filename = $_FILES["file"]["name"];
      $filesize = $_FILES["file"]["size"];
      $filetype = $_FILES["file"]["type"];
    
      $conn = new mysqli("localhost", "root", "", "filemanager");
      $conn->query("INSERT INTO files (name, size, type) VALUES ('$filename', $filesize, '$filetype')");
    
      $fileId = $conn->insert_id;
      $targetFile = $targetDirectory . $fileId;
      move_uploaded_file($_FILES["file"]["tmp_name"], $targetFile);
    
      $conn->close();
    
      header("Location: index.php");
    ?>
  3. delete.php

    <?php
      $fileId = $_GET["id"];
    
      $conn = new mysqli("localhost", "root", "", "filemanager");
      $result = $conn->query("SELECT * FROM files WHERE id = $fileId");
    
      if ($result->num_rows > 0) {
     $row = $result->fetch_assoc();
     $targetFile = "uploads/" . $row["id"];
     unlink($targetFile);
    
     $conn->query("DELETE FROM files WHERE id = $fileId");
      }
    
      $conn->close();
    
      header("Location: index.php");
    ?>

4. Run the project
After adding the above code to the corresponding file, access index.php in the browser to use the simple file management function. Users can upload files through the upload file form, and then view, download, and delete files in the file list.

Summary:
Through the introduction of this article, we have learned how to use PHP to develop simple file management functions. Using basic HTML forms and PHP code, we implemented file upload, download and delete functions. Using this example, you can extend and improve this file management system to better suit your needs. I hope this article will be helpful to your learning and development!

The above is the detailed content of How to use PHP to develop simple file management functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn