Back to FtpClient class

Method listNames

public string
listNames
(mixed $path = null)
Method to list the filenames of the contents of a directory on the FTP server
Parameters
  • string $path Path local file to store on the FTP server
Returns
  • string Directory listing
Since
  • 1.5
Class: FtpClient
Project: Joomla

Method listNames - Source code

/**
 * Method to list the filenames of the contents of a directory on the FTP server
 *
 * Note: Some servers also return folder names. However, to be sure to list folders on all
 * servers, you should use listDetails() instead if you also need to deal with folders
 *
 * @param   string  $path  Path local file to store on the FTP server
 *
 * @return  string  Directory listing
 *
 * @since   1.5
 */
public function listNames($path = null)
{
    $data = null;
    // If native FTP support is enabled let's use it...
    if (FTP_NATIVE) {
        // Turn passive mode on
        if (@ftp_pasv($this->_conn, true) === false) {
            Log::add(Text::sprintf('JLIB_CLIENT_ERROR_FTP_PASSIVE', __METHOD__), Log::WARNING, 'jerror');
            return false;
        }
        if (($list = @ftp_nlist($this->_conn, $path)) === false) {
            // Workaround for empty directories on some servers
            if ($this->listDetails($path, 'files') === array()) {
                return array();
            }
            Log::add(Text::sprintf('JLIB_CLIENT_ERROR_FTP_BAD_RESPONSE', __METHOD__), Log::WARNING, 'jerror');
            return false;
        }
        $list = preg_replace('#^' . preg_quote($path, '#') . '[/\\\\]?#', '', $list);
        if ($keys = array_merge(array_keys($list, '.'), array_keys($list, '..'))) {
            foreach ($keys as $key) {
                unset($list[$key]);
            }
        }
        return $list;
    }
    // If a path exists, prepend a space
    if ($path != null) {
        $path = ' ' . $path;
    }
    // Start passive mode
    if (!$this->_passive()) {
        Log::add(Text::sprintf('JLIB_CLIENT_ERROR_FTP_PASSIVE', __METHOD__), Log::WARNING, 'jerror');
        return false;
    }
    if (!$this->_putCmd('NLST' . $path, array(150, 125))) {
        @fclose($this->_dataconn);
        // Workaround for empty directories on some servers
        if ($this->listDetails($path, 'files') === array()) {
            return array();
        }
        Log::add(Text::sprintf('JLIB_CLIENT_ERROR_FTP_NOT_EXPECTED_RESPONSE_150_125', __METHOD__, $this->_response, $path), Log::WARNING, 'jerror');
        return false;
    }
    // Read in the file listing.
    while (!feof($this->_dataconn)) {
        $data .= fread($this->_dataconn, 4096);
    }
    fclose($this->_dataconn);
    // Everything go okay?
    if (!$this->_verifyResponse(226)) {
        Log::add(Text::sprintf('JLIB_CLIENT_ERROR_FTP_TRANSFER_FAILED', __METHOD__, $this->_response, $path), Log::WARNING, 'jerror');
        return false;
    }
    $data = preg_split('/[' . CRLF . ']+/', $data, -1, PREG_SPLIT_NO_EMPTY);
    $data = preg_replace('#^' . preg_quote(substr($path, 1), '#') . '[/\\\\]?#', '', $data);
    if ($keys = array_merge(array_keys($data, '.'), array_keys($data, '..'))) {
        foreach ($keys as $key) {
            unset($data[$key]);
        }
    }
    return $data;
}