
如何通过PHP编写一个简单的在线存储系统
在当前的数字化时代,数据的存储和管理变得至关重要。对于开发人员来说,搭建一个简单的在线存储系统能方便地保存和获取数据。本文将介绍如何使用PHP编写一个简单的在线存储系统,并提供具体的代码示例。
<!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><?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 '文件上传成功!';
}
?><?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>现在,你可以通过访问'index.php'上传文件,并通过访问'files.php'查看已上传的文件列表。
通过本文的步骤,你已经成功地编写了一个简单的在线存储系统。当然,在实际应用中还有许多功能需要进一步完善,如文件的下载、编辑和删除等。但这些都可以作为进一步的学习和扩展的方向。祝你编程愉快!
立即学习“PHP免费学习笔记(深入)”;
以上就是如何通过PHP编写一个简单的在线存储系统的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号