Home  >  Article  >  Backend Development  >  File upload and download using PHP and SQLite

File upload and download using PHP and SQLite

WBOY
WBOYOriginal
2023-07-28 13:03:17598browse

Using PHP and SQLite to implement file upload and download

Introduction:
In modern Internet applications, file upload and download are very common functions. This article will introduce how to use PHP and SQLite database to implement file upload and download functions. PHP is an open source scripting language, while SQLite is an embedded database engine that can be used directly in applications without a separate database server. By combining PHP and SQLite, we can easily implement file upload and download functions.

Step 1: Create database
First, create a SQLite database file named "files.db" in the root directory of the project. It can be created using SQLite's command line tools or using SQLite's API in PHP code.

<?php
// 连接数据库
$db = new SQLite3('files.db');

// 创建文件存储表
$query = 'CREATE TABLE IF NOT EXISTS files (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT,
    path TEXT
)';
$db->exec($query);
?>

Step 2: Upload files
Use an HTML form to allow users to upload files. In the form, we specify a file input field named "file" and use the POST method to submit the form to our PHP script.

<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" value="上传">
</form>

Then, we handle the file upload in the upload.php file. In this script, we first check whether the file upload is successful, then save the file in a specified directory on the server, and finally save the file information to the database.

<?php
// 连接数据库
$db = new SQLite3('files.db');

// 获取文件信息
$file = $_FILES['file'];
$fileName = $file['name'];
$fileTmp = $file['tmp_name'];
$fileSize = $file['size'];

// 检查文件上传是否成功
if ($fileSize > 0) {
    // 保存文件
    $filePath = 'files/' . $fileName;
    move_uploaded_file($fileTmp, $filePath);

    // 将文件信息保存到数据库
    $query = "INSERT INTO files (name, path) VALUES ('$fileName', '$filePath')";
    $db->exec($query);
}
?>

Step 3: Download the file
In order to implement the file download function, we first need to obtain the file information from the database and provide the user with a download link. In the file list page, we can use PHP code to query the database and generate a download link.

<?php
// 连接数据库
$db = new SQLite3('files.db');

// 查询数据库获取文件列表
$query = "SELECT * FROM files";
$result = $db->query($query);

// 生成文件列表
while ($row = $result->fetchArray()) {
    $fileName = $row['name'];
    $filePath = $row['path'];

    echo '<a href="'.$filePath.'" download>'.$fileName.'</a><br>';
}
?>

In the above code, we use the SELECT statement to retrieve all file information from the database. Then, use PHP's loop statement to output the download link of each file to the page. Note that we set the download attribute in the link, which will instruct the browser to download rather than preview the file.

Conclusion:
By using PHP and SQLite, we successfully implemented the file upload and download functions. We first created a SQLite database to store the file information, then used PHP to handle the file upload and save the file on the server and save the file information to the database. Finally, we can achieve the file download function by obtaining file information from the database to generate a download link.

This tutorial is just a simple example. In actual applications, you may also need to consider file security, file size restrictions, file type restrictions and other issues. But with this tutorial, you have taken the first step towards implementing file upload and download functionality. I hope this article can help you understand how to use PHP and SQLite to implement file upload and download functions.

The above is the detailed content of File upload and download using PHP and SQLite. 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