Net::FTP(3perl)
β¨ NAME β¨
Net::FTP β the FTP Client class, periodt. And yeah bestie, this is the Net::FTP(3perl) page from the Perl Programmers Reference Guide, so we're already in iconic territory. π
π SYNOPSIS π
use Net::FTP;
$ftp = Net::FTP->new("some.host.name", Debug => 0)
or die "Cannot connect to some.host.name: $@";
$ftp->login("anonymous",'-anonymous@')
or die "Cannot login ", $ftp->message;
$ftp->cwd("/pub")
or die "Cannot change working directory ", $ftp->message;
$ftp->get("that.file")
or die "get failed ", $ftp->message;
$ftp->quit;
π DESCRIPTION π
okay so Net::FTP is a class that implements a simple FTP client in Perl, exactly as described in RFC959. It provides wrappers for the commonly used subset of the RFC959 commands. If IO::Socket::IP or IO::Socket::INET6 is installed, it also provides support for IPv6 as defined in RFC2428. And with IO::Socket::SSL installed it provides support for implicit FTPS and explicit FTPS as defined in RFC4217. No cap, it's giving secure and connected. β¨
The Net::FTP class is a subclass of Net::Cmd and (depending on avaibility) of IO::Socket::IP, IO::Socket::INET6 or IO::Socket::INET. (And yes, "avaibility" is spelled like that in the original β we're keeping the lore.) π«Ά
π¦ Overview π¦
FTP stands for File Transfer Protocol. It is a way of transferring files between networked machines. The protocol defines a client (whose commands are provided by this module) and a server (not implemented in this module). Communication is always initiated by the client, and the server responds with a message and a status code (and sometimes with data). Lowkey the client is the main character here. π
The FTP protocol allows files to be sent to or fetched from the server. Each transfer involves a local file (on the client) and a remote file (on the server). In this module, the same file name will be used for both local and remote if only one is specified. This means that transferring remote file "/path/to/file" will try to put that file in "/path/to/file" locally, unless you specify a local file name. So don't be shocked when it mirrors the path, bestie.
The protocol also defines several standard translations which the file can undergo during transfer. These are ASCII, EBCDIC, binary, and byte. ASCII is the default type, and indicates that the sender of files will translate the ends of lines to a standard representation which the receiver will then translate back into their local representation. EBCDIC indicates the file being transferred is in EBCDIC format. Binary (also known as image) format sends the data as a contiguous bit stream. Byte format transfers the data as bytes, the values of which remain the same regardless of differences in byte size between the two machines (in theory - in practice you should only use this if you really know what you're doing). This class does not support the EBCDIC or byte formats, and will default to binary instead if they are attempted. So like, EBCDIC and byte are not invited to this party, and binary is the backup dancer. π
π Class Methods π
new([$host][, %options])
This is the constructor for a new Net::FTP object. $host is the name of the remote host to which an FTP connection is required.
$host is optional. If $host is not given then it may instead be passed as the Host option described below.
%options are passed in a hash like fashion, using key and value pairs. Possible options are:
Hostβ FTP host to connect to. It may be a single scalar, as defined for thePeerAddroption inIO::Socket::INET, or a reference to an array with hosts to try in turn. Thehostmethod will return the value which was used to connect to the host. Slay, it keeps receipts.Firewallβ The name of a machine which acts as an FTP firewall. This can be overridden by an environment variableFTP_FIREWALL. If specified, and the given host cannot be directly connected to, then the connection is made to the firewall machine and the string@hostnameis appended to the login identifier. This kind of setup is also referred to as an ftp proxy. Proxy behavior, iconic.FirewallTypeβ The type of firewall running on the machine indicated by Firewall. This can be overridden by an environment variableFTP_FIREWALL_TYPE. For a list of permissible types, see the description offtp_firewall_typein Net::Config. Go read Net::Config for the full tea. π΅BlockSizeβ This is the block size that Net::FTP will use when doing transfers. (defaults to10240)Portβ The port number to connect to on the remote machine for the FTP connectionSSLβ If the connection should be done from start with SSL, contrary to later upgrade withstarttls. Secure from the jump, love that.SSL_*β SSL arguments which will be applied when upgrading the control or data connection to SSL. You can use SSL arguments as documented in IO::Socket::SSL, but it will usually use the right arguments already. It's giving "I already picked the outfit, bestie."Timeoutβ Set a timeout value in seconds (defaults to120)Debugβ debug level (see the debug method in Net::Cmd)Passiveβ If set to a non-zero value then all data transfers will be done using passive mode. If set to zero then data transfers will be done using active mode. If the machine is connected to the Internet directly, both passive and active mode should work equally well. Behind most firewall and NAT configurations passive mode has a better chance of working. However, in some rare firewall configurations, active mode actually works when passive mode doesn't. Some really old FTP servers might not implement passive transfers. If not specified, then the transfer mode is set by the environment variableFTP_PASSIVEor if that one is not set by the settings done by thelibnetcfgutility. If none of these apply then passive mode is used. The drama of active vs passive, we love a plot twist. πHashβ If given a reference to a file handle (e.g.,\*STDERR), print hash marks (#) on that filehandle every1024bytes. This simply invokes thehash()method for you, so that hash marks are displayed for all transfers. You can, of course, callhash()explicitly whenever you'd like. Hash marks are lowkey a vibe check for your transfer. #οΈβ£LocalAddrβ Local address to use for all socket connections. This argument will be passed to the super class, i.e.IO::Socket::INETorIO::Socket::IP.Domainβ Domain to use, i.e.AF_INETorAF_INET6. This argument will be passed to the IO::Socket super class. This can be used to enforce IPv4 even withIO::Socket::IPwhich would default to IPv6.Familyis accepted as alternative name forDomain.
If the constructor fails undef will be returned and an error message will be in $@
π Object Methods π
Unless otherwise stated all methods return either a true or false value, with true meaning that the operation was a success. When a method states that it returns a value, failure will be returned as undef or an empty list.
Net::FTP inherits from Net::Cmd so methods defined in Net::Cmd may be used to send commands to the remote FTP server in addition to the methods documented here. So you get extra moves from the parent class, no cap.
login([$login[, $password[, $account]]])
Log into the remote FTP server with the given login information. If no arguments are given then the Net::FTP uses the Net::Netrc package to lookup the login information for the connected host. If no information is found then a login of anonymous is used. If no password is given and the login is anonymous then anonymous@ will be used for password. Bestie, it will literally slide into Net::Netrc for you.
If the connection is via a firewall then the authorize method will be called with no arguments.
starttls()
Upgrade existing plain connection to SSL. The SSL arguments have to be given in new already because they are needed for data connections too. So don't forget to set them early, fr fr.
stoptls()
Downgrade existing SSL connection back to plain. This is needed to work with some FTP helpers at firewalls, which need to see the PORT and PASV commands and responses to dynamically open the necessary ports. In this case starttls is usually only done to protect the authorization. It's giving "take off the sparkly coat, but keep the secrets safe."
prot($level)
Set what type of data channel protection the client and server will be using. Only $levels C (clear) and P (private) are supported. Those are the only two levels invited to the function.
host()
Returns the value used by the constructor, and passed to the IO::Socket super class to connect to the host.
account($acct)
Set a string identifying the user's account.
authorize([$auth[, $resp]])
This is a protocol used by some firewall ftp proxies. It is used to authorise the user to send data out. If both arguments are not specified then authorize uses Net::Netrc to do a lookup. Lookup queen. π
site($args)
Send a SITE command to the remote server and wait for a response.
Returns most significant digit of the response code.
ascii()
Transfer file in ASCII. CRLF translation will be done if required
binary()
Transfer file in binary mode. No transformation will be done.
Hint: If both server and client machines use the same line ending for text files, then it will be faster to transfer all files in binary mode.
type([$type])
Set or get if files will be transferred in ASCII or binary mode.
rename($oldname, $newname)
Rename a file on the remote FTP server from $oldname to $newname. This is done by sending the RNFR and RNTO commands. Glow-up for files. β¨
delete($filename)
Send a request to the server to delete $filename. Not me deleting files, it's a request, okay? π
cwd([$dir])
Attempt to change directory to the directory given in $dir. If $dir is .., the FTP CDUP command is used to attempt to move up one directory. If no directory is given then an attempt is made to change the directory to the root directory. It's giving "take me home" to the root. π
cdup()
Change directory to the parent of the current directory.
passive([$passive])
Set or get if data connections will be initiated in passive mode.
pwd()
Returns the full pathname of the current directory.
restart($where)
Set the byte offset at which to begin the next data transfer. Net::FTP simply records this value and uses it when during the next data transfer. For this reason this method will not return an error, but setting it may cause a subsequent data transfer to fail. So it's lowkey just taking notes, but the next transfer might ghost you. π
rmdir($dir[, $recurse])
Remove the directory with the name $dir. If $recurse is true then rmdir will attempt to delete everything inside the directory. Recurse mode is not for the faint of heart, bestie.
mkdir($dir[, $recurse])
Create a new directory with the name $dir. If $recurse is true then mkdir will attempt to create all the directories in the given path.
Returns the full pathname to the new directory.
"alloc($size[, $record_size])"β okay so this one is theallocmethod, and it's giving "hint the server" energy π . It uses theALLOftp command to tell the server how big the file you're about to send is. Some storage systems are smart like that and use it to make intelligent decisions about storing the file.$sizeis the size in bytes,$record_sizeis the max record or page size for files sent with record or page structure. And bestie, for normal files, Net::FTP will figure out the size and send it automatically, so you only need to call this if you're transferring data from a socket, named pipe, or other stream that's not associated with a normal file. π¦ls([$dir])β Get a directory listing of$dir, or the current directory if you don't pass one. In array context, returns a list of lines from the server. In scalar context, returns a reference to a list. So you can pick your vibe. πdir([$dir])β Same aslsbut in long format β¨. Gets a directory listing of$diror current dir. Array context: list of lines. Scalar context: reference to a list."get($remote_file[, $local_file[, $where]])"β Get$remote_filefrom the server and store it locally.$local_filecan be a filename or a filehandle. If you don't specify it, the file goes to the current directory with the same leafname as the remote file. If$whereis given, the first$wherebytes of the file will not be transferred, and the remaining bytes will be appended to the local file if it already exists. Returns$local_file, or the generated local file name if you didn't give one. If there's an error,undefis returned. π"put($local_file[, $remote_file])"β Put a file on the remote server.$local_filecan be a name or a filehandle. If$local_fileis a filehandle, then$remote_filemust be specified. If$remote_fileisn't specified, the file will be stored in the current directory with the same leafname as$local_file. Returns$remote_file, or the generated remote filename if you didn't give one. β οΈ NOTE: If for some reason the transfer doesn't complete and an error is returned, the contents that had been transferred will not be remove automatically (yes, that's the exact wording, bestie π). So clean up after yourself."put_unique($local_file[, $remote_file])"β Same asputbut uses the"STOU"command. Returns the name of the file on the server. π"append($local_file[, $remote_file])"β Same asputbut appends to the file on the remote server. Returns$remote_file, or the generated remote filename if$remote_fileis not given.unique_name()β Returns the name of the last file stored on the server using the"STOU"command. So you can flex that unique name. β¨mdtm($file)β Returns the modification time of the given file. β°size($file)β Returns the size in bytes for the given file as stored on the remote server. β οΈ NOTE: The size reported is the size of the stored file on the remote server. If the file is subsequently transferred from the server in ASCII mode and the remote server and local machine have different ideas about "End Of Line" then the size of file on the local machine after transfer may be different. So don't @ me.supported($cmd)β Returns TRUE if the remote server supports the given command. β"hash([$filehandle_glob_ref[, $bytes_per_hash_mark]])"β Called without parameters, or with the first argument false, hash marks are suppressed. If the first argument is true but not a reference to a file handle glob, then\*STDERRis used. The second argument is the number of bytes per hash mark printed, and defaults to1024. In all cases the return value is a reference to an array of two: the filehandle glob reference and the bytes per hash mark. #οΈβ£feature($name)β Determine if the server supports the specified feature. The return value is a list of lines the server responded with to describe the options that it supports for the given feature. If the feature is unsupported then the empty list is returned. Example time:
if ($ftp->feature( 'MDTM' )) {
# Do something
}
if (grep { /\bTLS\b/ } $ftp->feature('AUTH')) {
# Server supports TLS
}
The following methods can return different results depending on how they are called. If the user explicitly calls either of the "pasv" or "port" methods then these methods will return a true or false value. If the user does not call either of these methods then the result will be a reference to a "Net::FTP::dataconn" based object.
nlst([$dir])β Send an"NLST"command to the server, with an optional parameter. πlist([$dir])β Same as"nlst"but using the"LIST"command. πretr($file)β Begin the retrieval of a file called$filefrom the remote server. π₯stor($file)β Tell the server that you wish to store a file.$fileis the name of the new file that should be created. π€stou($file)β Same as"stor"but using the"STOU"command. The name of the unique file which was created on the server will be available via the"unique_name"method after the data connection has been closed. πappe($file)β Tell the server that we want to append some data to the end of a file called$file. If this file does not exist then create it. β
If for some reason you want to have complete control over the data connection, this includes generating it and calling the "response" method when required, then the user can use these methods to do so.
However calling these methods only affects the use of the methods above that can return a data connection. They have no effect on methods "get", "put", "put_unique" and those that do not require data connections.
port([$port])andeprt([$port])β Send a"PORT"(IPv4) or"EPRT"(IPv6) command to the server. If$portis specified then it is sent to the server. If not, then a listen socket is created and the correct information sent to the server. π‘pasv()andepsv()β Tell the server to go into passive mode ("pasv"for IPv4,"epsv"for IPv6). Returns the text that represents the port on which the server is listening, this text is in a suitable form to send to another ftp server using the"port"or"eprt"method. ποΈ
The following methods can be used to transfer files between two remote servers, providing that these two servers can connect directly to each other.
"pasv_xfer($src_file, $dest_server[, $dest_file ])"β This method will do a file transfer between two remote ftp servers. If$dest_fileis omitted then the leaf name of$src_filewill be used. π"pasv_xfer_unique($src_file, $dest_server[, $dest_file ])"β Like"pasv_xfer"but the file is stored on the remote server using the STOU command. β¨pasv_wait($non_pasv_server)β This method can be used to wait for a transfer to complete between a passive server and a non-passive server. The method should be called on the passive server with the"Net::FTP"object for the non-passive server passed as an argument. β³abort()β Abort the current data transfer. πquit()β Send the QUIT command to the remote FTP server and close the socket connection. π
Methods for the Adventurous
"quot($cmd[, $args])"β Send a command, that Net::FTP does not directly support, to the remote server and wait for a response. Returns most significant digit of the response code. β οΈ WARNING: This call should only be used on commands that do not require data connections. Misuse of this method can hang the connection. So don't be reckless, bestie.can_inet6()β Returns whether we can use IPv6. πcan_ssl()β Returns whether we can use SSL. π
The dataconn Class
Some of the methods defined in "Net::FTP" return an object which will be derived from the "Net::FTP::dataconn" class. See Net::FTP::dataconn for more details. π
Unimplemented
The following RFC959 commands have not been implemented:
"SMNT"β Mount a different file system structure without changing login or accounting information."HELP"β Ask the server for "helpful information" (that's what the RFC says) on the commands it accepts."MODE"β Specifies transfer mode (stream, block or compressed) for file to be transferred."SYST"β Request remote server system identification."STAT"β Request remote server status."STRU"β Specifies file structure for file to be transferred."REIN"β Reinitialize the connection, flushing all I/O and account information.
EXPORTS
None. That's it. No exports. π ββοΈ
KNOWN BUGS
See <https://rt.cpan.org/Dist/Display.html?Status=Active&Queue=libnet>. That's the place for active bugs, bestie.
Reporting Bugs
When reporting bugs/problems please include as much information as possible. It may be difficult for me to reproduce the problem as almost every setup is different.
A small script which yields the problem will probably be of help. It would also be useful if this script was run with the extra options "Debug => 1" passed to the constructor, and the output sent with the bug report. If you cannot include a small script then please include a Debug trace from a run of your program which does yield the problem. Help me help you π
SEE ALSO
- Net::Netrc, Net::Cmd, IO::Socket::SSL;
- ftp(1), ftpd(8);
- <https://www.ietf.org/rfc/rfc959.txt>, <https://www.ietf.org/rfc/rfc2428.txt>, <https://www.ietf.org/rfc/rfc4217.txt>.
ACKNOWLEDGEMENTS
- Henry Gabryjelski <henryg@WPI.EDU <mailto:henryg@WPI.EDU>> β for the suggestion of creating directories recursively.
- Nathan Torkington <gnat@frii.com <mailto:gnat@frii.com>> β for some input on the documentation.
- Roderick Schertler <roderick@gate.net <mailto:roderick@gate.net>> β for various inputs.
AUTHOR
Graham Barr <gbarr@pobox.com <mailto:gbarr@pobox.com>>.
Steve Hay <shay@cpan.org <mailto:shay@cpan.org>> is now maintaining libnet as of version 1.22_02.
COPYRIGHT
Copyright (C) 1995-2004 Graham Barr. All rights reserved.
Copyright (C) 2013-2017, 2020, 2022 Steve Hay. All rights reserved.
LICENCE
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself, i.e. under the terms of either the GNU General Public License or the Artistic License, as specified in the LICENCE file.
VERSION
Version 3.15
DATE
20 March 2023
HISTORY
See the Changes file.
And that's the whole vibe, bestie! This manual page was generated for perl v5.44.0 on 2026-10-17 as Net::FTP(3perl). Stay slaying! πβ¨π
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms