How to write a simple online storage system through PHP

WBOY
Release: 2023-09-24 08:46:01
Original
1344 people have browsed it

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.

  1. 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.
  2. 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)
  1. 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>文件上传</h1>
    <form enctype="multipart/form-data" action="upload.php" method="POST">
        <input type="file" name="file" required>
        <input type="submit" value="上传">
    </form>
</body>
</html>
Copy after login
  1. 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 '文件上传成功!';
}
?>
Copy after login
  1. 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>文件列表</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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template