0

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?

mikatakana
  • 503
  • 9
  • 22

2 Answers2

2

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.

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • Each admin enter ftp-options for his server on my site. And he can select number of connections. But I see, that they often have mistakes in this value. And I think about automatization of this work. So I can try to send 50 simple requests to server then 20, then 10 and etc. For example, getting the files list in root folder with `CURLOPT_FTPLISTONLY = 1` – mikatakana Jan 23 '14 at 17:23
  • It's seems a good option. This way you'll be able to really know how many simultaneous ftp connections you're allowed. please post your results after. – Pedro Lobito Jan 23 '14 at 17:31
  • @mikatakana if you managed to fix your problem, can you mark the question as correct ? thanks. – Pedro Lobito Jan 23 '14 at 19:47
  • you can see my solution [by this link](http://stackoverflow.com/questions/21310816/how-to-get-ftp-max-connections-via-curl-in-php/21349419#21349419) – mikatakana Jan 25 '14 at 10:35
0

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.

mikatakana
  • 503
  • 9
  • 22