Implement file upload and download in Workerman documents

WBOY
Release: 2023-11-08 18:02:16
Original
1006 people have browsed it

Implement file upload and download in Workerman documents

To implement file upload and download in Workerman documents, specific code examples are required

Introduction:
Workerman is a high-performance PHP asynchronous network communication framework. It has the characteristics of simplicity, efficiency and ease of use. In actual development, file uploading and downloading are common functional requirements. This article will introduce how to use the Workerman framework to implement file uploading and downloading, and give specific code examples.

1. File upload:

File upload refers to the operation of transferring files on the local computer to the server. The following is a specific code example for using Workerman to implement file upload:

  1. Create server-side code
onMessage = function($connection, $data) { if(isset($data['type']) && $data['type'] === 'file'){ if (isset($data['content']) && isset($data['fileName'])) { file_put_contents($data['fileName'], $data['content']); $connection->send('文件上传成功!'); } else { $connection->send('文件上传失败!'); } } }; Worker::runAll();
Copy after login
  1. Create client-side code
onConnect = function() use ($connection){ $connection->send(array('type' => 'file', 'content' => file_get_contents('test.txt'), 'fileName' => 'test.txt')); }; $connection->onMessage = function($connection, $data){ echo $data . PHP_EOL; }; $connection->connect(); AsyncWorker::runAll();
Copy after login

In the above code, the server is bound to a TCP port. When there is a connection, it determines whether the transmitted data is file content. If so, the file content is written to the file. After the client connects to the server, it sends the contents of the local test.txt file to the server.

2. File download:

File download refers to the operation of transferring files from the server to the local computer. The following is a specific code example for using Workerman to implement file downloading:

  1. Create server-side code
onMessage = function($connection, $data) { if(isset($data['type']) && $data['type'] === 'file'){ if (isset($data['fileName']) && file_exists($data['fileName'])) { $connection->send(file_get_contents($data['fileName'])); } else { $connection->send('文件不存在!'); } } }; Worker::runAll();
Copy after login
  1. Create client-side code
onConnect = function() use ($connection){ $connection->send(array('type' => 'file', 'fileName' => 'test.txt')); }; $connection->onMessage = function($connection, $data){ file_put_contents('download.txt', $data); echo '文件下载成功!' . PHP_EOL; }; $connection->connect(); AsyncWorker::runAll();
Copy after login

In the above code, the server is bound to a TCP port. When there is a connection, it determines whether the transmitted data is a file name and whether the file exists. If it exists, the file content is sent to the client. After the client connects to the server, it sends the name of the file to be downloaded to the server. The server sends the file content to the client and writes the content to the local download.txt file.

Conclusion:
Through the above code examples, we can see that it is very simple to use the Workerman framework to upload and download files. Through simple configuration and writing code, we can easily implement file upload and download functions, improve the user experience of web applications and improve development efficiency. Hope this article is helpful to you.

The above is the detailed content of Implement file upload and download in Workerman documents. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!