Home > PHP Framework > ThinkPHP > How to upload txt to database in thinkphp

How to upload txt to database in thinkphp

WBOY
Release: 2023-05-26 09:53:37
Original
732 people have browsed it

thinkphp is an excellent PHP development framework that provides many convenient functions, including file upload. In this article, we will discuss how to upload txt files to database using thinkphp.

  1. Create database table

First, we need to create a database table to store the uploaded txt file. In this example, we will create a table called "txt_data" that contains two fields: "id" and "content". Among them, "id" is the primary key, which is automatically incremented, and "content" is the field used to store the content of the txt file.

  1. Create upload form

Next, we will create an upload form so that users can select a txt file to upload. In this form, we wrap the file upload input box with the "form" tag.

  1. Processing upload requests

When the user selects the txt file to be uploaded and submits the form, the upload request needs to be processed on the server side. For the thinkphp framework, you can use the "Request" object to obtain uploaded files.

  1. Read the content of the file and store it in the database

After getting the uploaded file, we need to read the content of the file and store it in the database middle. For txt files, we can use PHP's built-in "file_get_contents()" function to read the file contents. Then, we can use the "Db" class encapsulated by thinkphp to insert a new record into the database.

The following is the complete upload code:

(1) Create database table

CREATE TABLE txt_data (
id int(11) NOT NULL AUTO_INCREMENT,
content text NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET= utf8mb4;

(2) Create upload form


< input type="file" name="txt_file">

(3) Processing Upload request

public function upload()
{

$file = request()->file('txt_file');
$content = file_get_contents($file->getRealPath());

$data = [
   'content' => $content
];

$result = Db::table('txt_data')->insert($data);

if ($result) {
    return '上传成功';
} else {
    return '上传失败';
}
Copy after login

}

Summary:

In this article, we learned how to use the thinkphp framework Upload the txt file to the database. Specifically, we created a database table named "txt_data" to store the uploaded txt file content, then created an upload form and processed the upload request on the server side. Finally, we use the "file_get_contents()" function to read the uploaded txt file contents and store it into the database using the "Db" class.

The above is the detailed content of How to upload txt to database in thinkphp. 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