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

4.x is outdated, please check the latest version 22.x


Latest version: pecl install openswoole-22.1.2

Declaration

<?php Swoole\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 Swoole\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 August 31, 2022