How to write a simple online storage system through PHP
How to write a simple online storage system through PHP
In the current digital era, data storage and management have become crucial. For developers, building a simple online storage system can easily save and retrieve data. This article will introduce how to use PHP to write a simple online storage system and provide specific code examples.
- Preparation
Before you begin, make sure you have installed a PHP interpreter and a Web server (such as Apache), and have configured the relevant environment. - Database Design
We will use MySQL as the database to store data. First, create a database and create a table named 'files' in it with the following fields:
- id (auto-incrementing primary key)
- name (File name)
- size (File size)
- content (File content)
- created_at (Creation time)
- updated_at (Updated time)
- Create file upload form
First, we need to create a form to allow users to upload files. Create a file called 'index.php' and add the following code in it:
<!DOCTYPE html> <html> <head> <title>文件上传</title> </head> <body> <h1 id="文件上传">文件上传</h1> <form enctype="multipart/form-data" action="upload.php" method="POST"> <input type="file" name="file" required> <input type="submit" value="上传"> </form> </body> </html>
- Handling file uploads
Create a file called 'upload.php', And add the following code in it:
<?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { $file = $_FILES['file']; $fileName = $file['name']; $fileSize = $file['size']; $fileContent = file_get_contents($file['tmp_name']); // 连接数据库并保存文件信息 $conn = new mysqli('localhost', '用户名', '密码', '数据库名'); $sql = "INSERT INTO files (name, size, content, created_at, updated_at) VALUES ('$fileName', '$fileSize', '$fileContent', now(), now())"; $conn->query($sql); $conn->close(); echo '文件上传成功!'; } ?>
- Show uploaded files
In order to be able to view the uploaded files, we need to create a page for displaying the file list. Create a file called 'files.php' and add the following code in it:
<?php // 连接数据库并获取文件列表 $conn = new mysqli('localhost', '用户名', '密码', '数据库名'); $sql = "SELECT * FROM files"; $result = $conn->query($sql); $files = $result->fetch_all(MYSQLI_ASSOC); $conn->close(); ?> <!DOCTYPE html> <html> <head> <title>文件列表</title> </head> <body> <h1 id="文件列表">文件列表</h1> <table> <tr> <th>ID</th> <th>文件名称</th> <th>文件大小</th> <th>创建时间</th> </tr> <?php foreach ($files as $file): ?> <tr> <td><?php echo $file['id']; ?></td> <td><?php echo $file['name']; ?></td> <td><?php echo $file['size']; ?></td> <td><?php echo $file['created_at']; ?></td> </tr> <?php endforeach; ?> </table> </body> </html>
Now you can upload files by accessing 'index.php' and add them by accessing 'files.php 'View the list of uploaded files.
Through the steps in this article, you have successfully written a simple online storage system. Of course, there are still many functions that need to be further improved in practical applications, such as file downloading, editing and deletion. But these can all serve as directions for further learning and expansion. Happy programming!
The above is the detailed content of How to write a simple online storage system through PHP. For more information, please follow other related articles on the PHP Chinese website!

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
