OpenSwoole\Http\Request->files

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\Http\Request->files: array

Parameters

None

Return

Array

Description

The OpenSwoole\Http\Request->files class property is an array which contains the information for HTTP POST files that were sent to the server.

All the keys of this array are lowercase. You use this array to interact with uploaded files from the client, it is equivalent to the PHP super global variable $_FILES but you should use OpenSwoole\Http\Request->files instead.

The OpenSwoole\Http\Request->files array is usually used when a client uploads a file from a HTML form for example. This array is formatted to be a 2D array based on the uploaded file name, which you can access details about the uploaded file.

Example

<?php

$server->on('Request', function(OpenSwoole\Server\Request $request, OpenSwoole\Server\Response $response)
{
    var_dump($request->files['profile_image']);
});

Output from an uploaded file

  • name: The name of the file from the client, for example my_profile_image.jpg
  • type: The file MIME type, for example image/jpeg
  • tmp_name: The area where the uploaded file is stored before being saved permanently, for example /tmp/swoole.upfile.n3FmFr, this file is generated by OpenSwoole so you can process the file, this directory can be set in the server configuration
  • error: An error code which indicates any upload errors, 0 for successful upload
  • size: The size of the uploaded file in bytes

Notes

  • The maximum file size must not exceed the value set by package_max_length

  • Do not use the OpenSwoole server to handle large file uploads, instead you will need to perform chunk uploads or file streaming

  • When the request ends, the $request object is destroyed and so are any temporary uploaded files

Last updated on September 20, 2022