简体   繁体   中英

How to get FTP max connections via cURL in PHP?

I'm working with some FTP-servers with PHP cURL multi handlers to download imagers from they by multiconnections.

And I want to know, how many connections supports each server. How can I do this with curl library?

You won't be able to determine how many simultaneous connections are allowed to a certain FTP server unless you're the administrator.

Partial solution 1: As an alternative you can try to make, let's say, 50 simultaneous connections to the ftp and check when the server issues an error max connections (#) exceeded

Partial Solution 2: Contact the ftp administrator as ask him how many simultaneous connections you're entitled to.

My temporary solution

<?php

$params = [
    'host'      => '{FTP_SERVER}',
    'user'      => '{FTP_LOGIN}',
    'password'  => '{FTP_PASSWORD}',
    'streams'   => {FTP_CONNECTIONS_COUNT}
];

$defaults = [
    CURLOPT_HEADER          => 1,
    CURLOPT_RETURNTRANSFER  => 0,
    CURLOPT_NOBODY          => 1,
    CURLOPT_USERPWD         => $params['user'] . ':' . $params['password'],
];

$mh = curl_multi_init();
$streams = [];

// Generate N requests to FTP-server
$tasks = array_fill(0, $params['streams'], $params['host'] . '/');

// Create streams
foreach ($tasks as $n => $task) {
    $ch = curl_init();
    curl_setopt_array($ch, $defaults + [
        CURLOPT_URL         => $task,
        CURLOPT_FTPLISTONLY => 0,
        CURLOPT_VERBOSE     => 0
    ]);

    curl_multi_add_handle($mh, $ch);
    $streams[$n] = $ch; 
}

// Process streams
$isAvailable = true;
do {
    curl_multi_exec($mh, $running);

    // Break if we have server error
    if ($i = curl_multi_info_read($mh) and curl_getinfo($i['handle'], CURLINFO_HTTP_CODE) > 500) {
        $isAvailable = false;
        break;
    }

    curl_multi_select($mh);
} while ($running > 0);

// Close streams
foreach ($streams as $ch) {
    curl_multi_remove_handle($mh, $ch);
    curl_close($ch);
}

curl_multi_close($mh);

// Print result
var_dump($isAvailable);

Please, correct me if I'm wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM