Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
[bug?] FTP class '$ftp->close();'
  • Is it okay if I don't code '$ftp->close();' when uploading a file using the FTP protocol in the FTP class?
    I would appreciate any advice on how to deal with any problems.


    [Wants]

    I want to upload a file using the FTP protocol in the FTP class.

    Set the setting file as below,

    src/web/fuel/core/config/ftp.php

    ’test' => array(
            'hostname' => 'test-server',
            'username' => '****',
            'password' =>  '****',
            'port' => 21,
            'passive'  => true,
            'ssl_mode'  => false,
            'debug' => true,

    ),

    I think, need to Code according to the following procedurein a function that uploads multiple. 

    Specifications that repeat processing for each file.

    Try {

            $ftp = Ftp::forgetest');
            $ftp ->connect();
            $ftp->upload('/local/path/to/myfile.html', '/public_html/myfile.html', ‘binary’);
            $ftp->close();
    } catch(\Exception $e) {
            // error
    }


    [Problem]
    Test result:
     I can disconnect with '''$ftp->close();'''but the process does not end there, and the process of disconnecting again at the end of the function runs, resulting in an error.

    Value at the time of disconnection processing

    '$ftp->close();' -> _conn_id:resource id='12' type='FTP Buffer’

    When disconnecting again at the end of the function -> _conn_id:resource id='12' type='Unknown'

    In addition, when I tested without adding  '''$ftp->close();''' no error occurred and the process was as expected.

     What to do if it's not a bug

  • HarroHarro
    Accepted Answer
    I don't understand what you mean with "the process does not end there".

    FTP is an interactief protocol (like SNMP, POP3, IMAP, etc), to which you issue commands to do things. This is also why FTP has a control and a data port, so you keep having access (control) while data is being transferred.

    So the sequence is
    CONNECT
    COMMAND 1
    COMMAND 2
    COMMAND 3
    COMMAND 4
    COMMAND 5
    DISCONNECT

    So if you need to do multiple things, like upload 4 files, you connect once at the beginning, close the connection at the end, and no where else.

    From the top of my head something like:

    $files = [
        '/local/path/to/file1.html' => '/public_html/file1.html',
        '/local/path/to/file2.html' => '/public_html/file2.html',
        '/local/path/to/file3.html' => '/public_html/file3.html',
        '/local/path/to/file4.html' => '/public_html/file4.html',
    ];

    if ($ftp - Ftp::forge('test'))
    {
        if ($ftp->connect())
        {
            foreach ($files as $src => $dest)
            {
                if ( ! $ftp->upload($src, $dest, 'binary'))
               {
                   // upload failed
               }
            }
            $ftp->close();
        }
        else
        {
            // connect failed
        }
    }
    else
    {
        // forge failed
    }
  • Thank you for your reply.
    I was connecting and disconnecting every time I uploaded a file.
    I will try according to your advice and I will report later

  • That should also work, but is less efficient.

    And in that case it would be best to forge the object at the beginning of your code so you can reuse the object, and connect and close outside of your try/catch code so the connection is always closed.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion