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:
- index.php: Main page, used to display the upload form and file list.
- upload.php: Used to handle file uploads.
- 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
index.php
<!DOCTYPE html> <html> <head> <title>文件管理</title> </head> <body> <h1 id="文件管理">文件管理</h1> <h2 id="上传文件">上传文件</h2> <form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="上传"> </form> <h2 id="文件列表">文件列表</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>
Copy after loginupload.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"); ?>
Copy after logindelete.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"); ?>
Copy after login4. 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The hard disk serial number is an important identifier of the hard disk and is usually used to uniquely identify the hard disk and identify the hardware. In some cases, we may need to query the hard drive serial number, such as when installing an operating system, finding the correct device driver, or performing hard drive repairs. This article will introduce some simple methods to help you check the hard drive serial number. Method 1: Use Windows Command Prompt to open the command prompt. In Windows system, press Win+R keys, enter "cmd" and press Enter key to open the command

In web development, we often need to use caching technology to improve website performance and response speed. Memcache is a popular caching technology that can cache any data type and supports high concurrency and high availability. This article will introduce how to use Memcache in PHP development and provide specific code examples. 1. Install Memcache To use Memcache, we first need to install the Memcache extension on the server. In CentOS operating system, you can use the following command

How to write a simple student performance report generator using Java? Student Performance Report Generator is a tool that helps teachers or educators quickly generate student performance reports. This article will introduce how to use Java to write a simple student performance report generator. First, we need to define the student object and student grade object. The student object contains basic information such as the student's name and student number, while the student score object contains information such as the student's subject scores and average grade. The following is the definition of a simple student object: public

Where can I find file management on Xiaomi mobile phones? There is a file management function in Xiaomi mobile phones, but most users do not know how to find file management. Next is the tutorial on how to open file management on Xiaomi mobile phones brought by the editor. If you are interested, Users come and take a look! Where to find file management on Xiaomi mobile phone? 1. First open [Settings] in Xiaomi mobile phone, enter the page and slide to find the [Desktop] option; 2. Then on the desktop function page, slide the button behind the [Desktop Search Box]; 3. Finally, in File management functions can be found on the desktop.

How to write a simple music recommendation system in C++? Introduction: Music recommendation system is a research hotspot in modern information technology. It can recommend songs to users based on their music preferences and behavioral habits. This article will introduce how to use C++ to write a simple music recommendation system. 1. Collect user data First, we need to collect user music preference data. Users' preferences for different types of music can be obtained through online surveys, questionnaires, etc. Save data in a text file or database

How to write a simple minesweeper game in C++? Minesweeper is a classic puzzle game that requires players to reveal all the blocks according to the known layout of the minefield without stepping on the mines. In this article, we will introduce how to write a simple minesweeper game using C++. First, we need to define a two-dimensional array to represent the map of the Minesweeper game. Each element in the array can be a structure used to store the status of the block, such as whether it is revealed, whether there are mines, etc. In addition, we also need to define

How to implement version control and code collaboration in PHP development? With the rapid development of the Internet and the software industry, version control and code collaboration in software development have become increasingly important. Whether you are an independent developer or a team developing, you need an effective version control system to manage code changes and collaborate. In PHP development, there are several commonly used version control systems to choose from, such as Git and SVN. This article will introduce how to use these tools for version control and code collaboration in PHP development. The first step is to choose the one that suits you

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.
