OpenSwoole\Coroutine\Http\Client->addFile(...)

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

Declaration

<?php OpenSwoole\Coroutine\Http\Client->addFile(string $path, string $name, string $mimeType = null, string $fileName = null, int $offset = 0, int $length = 0): void

Parameters

path

The absolute location of the file, cannot be an empty or non-existent file

name

Used for the name of the form for the HTTP FILES parameter

mimeType

Set the Mime content type format of the file like image/jpeg, optional

fileName

Set the name of the file being sent, optional, will default to the file basename($path) if not set

offset

The offset of where to start from, allows you to upload parts of a file, useful for resumable transfers, default is the start of the file

length

The length of the file to send in relation to the offset, the default is the entire file

Return

None


Description

Attach files to the HTTP request. Allows you to send a POST request with a file.

Using this method will automatically use the POST HTTP method and change the header Content-Type to form-data. This method also has support for asynchronous file sending for large files, it is based on sendfile.


Example

<?php
use OpenSwoole\Coroutine\Http\Client;

co::run(function()
{
    $client = new Client('127.0.0.1', 80);

    $client->setHeaders([
        'Host' => 'localhost',
    ]);

    // Never timeout the request
    $client->set(['timeout' => -1]);

    $client->addFile('/var/www/example/files/cat.png', 'profileImage', 'image/png');

    $client->post('/upload/image', ['age' => 23]);

    echo $client->status . "\n";

    var_dump($client->body);

    $client->close();
});
Last updated on September 1, 2022