src/Clients/FtpClient.php
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)
			{
				throw new FilesystemException(__METHOD__ . ': Unable to use passive mode.');
			}

			if (($list = @ftp_nlist($this->conn, $path)) === false)
			{
				// Workaround for empty directories on some servers
				if ($this->listDetails($path, 'files') === array())
				{
					return array();
				}

				throw new FilesystemException(__METHOD__ . 'Bad response.');
			}

			$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())
		{
			throw new FilesystemException(__METHOD__ . ': Unable to use passive mode.');
		}

		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();
			}

			throw new FilesystemException(
				sprintf('%1$s: Bad response.  Server response: %2$s [Expected: 150 or 125].  Path sent: %3$s', __METHOD__, $this->response, $path)
			);
		}

		// Read in the file listing.
		while (!feof($this->dataconn))
		{
			$data .= fread($this->dataconn, 4096);
		}

		fclose($this->dataconn);

		// Everything go okay?
		if (!$this->_verifyResponse(226))
		{
			throw new FilesystemException(
				sprintf('%1$s: Transfer failed.  Server response: %2$s [Expected: 226].  Path sent: %3$s', __METHOD__, $this->response, $path)
			);
		}

		$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;
	}
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)
			{
				throw new FilesystemException(__METHOD__ . ': Unable to use passive mode.');
			}

			if (($list = @ftp_nlist($this->conn, $path)) === false)
			{
				// Workaround for empty directories on some servers
				if ($this->listDetails($path, 'files') === array())
				{
					return array();
				}

				throw new FilesystemException(__METHOD__ . 'Bad response.');
			}

			$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())
		{
			throw new FilesystemException(__METHOD__ . ': Unable to use passive mode.');
		}

		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();
			}

			throw new FilesystemException(
				sprintf('%1$s: Bad response.  Server response: %2$s [Expected: 150 or 125].  Path sent: %3$s', __METHOD__, $this->response, $path)
			);
		}

		// Read in the file listing.
		while (!feof($this->dataconn))
		{
			$data .= fread($this->dataconn, 4096);
		}

		fclose($this->dataconn);

		// Everything go okay?
		if (!$this->_verifyResponse(226))
		{
			throw new FilesystemException(
				sprintf('%1$s: Transfer failed.  Server response: %2$s [Expected: 226].  Path sent: %3$s', __METHOD__, $this->response, $path)
			);
		}

		$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;
	}
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 ) { throw new FilesystemException ( __METHOD__ . ': Unable to use passive mode.' ) ; } if ( ( $ list = @ ftp_nlist ( $ this -> conn , $ path ) ) === false ) { // Workaround for empty directories on some servers if ( $ this -> listDetails ( $ path , 'files' ) === array ( ) ) { return array ( ) ; } throw new FilesystemException ( __METHOD__ . 'Bad response.' ) ; } $ 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 ( ) ) { throw new FilesystemException ( __METHOD__ . ': Unable to use passive mode.' ) ; } 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 ( ) ; } throw new FilesystemException ( sprintf ( '%1$s: Bad response.  Server response: %2$s [Expected: 150 or 125].  Path sent: %3$s' , __METHOD__ , $ this -> response , $ path ) ) ; } // Read in the file listing. while ( ! feof ( $ this -> dataconn ) ) { $ data .= fread ( $ this -> dataconn , 4096 ) ; } fclose ( $ this -> dataconn ) ; // Everything go okay? if ( ! $ this -> _verifyResponse ( 226 ) ) { throw new FilesystemException ( sprintf ( '%1$s: Transfer failed.  Server response: %2$s [Expected: 226].  Path sent: %3$s' , __METHOD__ , $ this -> response , $ path ) ) ; } $ 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 ; }
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.0
@throws  FilesystemException
Method to list the filenames of the contents of a directory on the FTP server