1Net::FTP(3pm) Perl Programmers Reference Guide Net::FTP(3pm)
2
3
4
6 Net::FTP - FTP Client class
7
9 use Net::FTP;
10
11 $ftp = Net::FTP->new("some.host.name", Debug => 0)
12 or die "Cannot connect to some.host.name: $@";
13
14 $ftp->login("anonymous",'-anonymous@')
15 or die "Cannot login ", $ftp->message;
16
17 $ftp->cwd("/pub")
18 or die "Cannot change working directory ", $ftp->message;
19
20 $ftp->get("that.file")
21 or die "get failed ", $ftp->message;
22
23 $ftp->quit;
24
26 "Net::FTP" is a class implementing a simple FTP client in Perl as
27 described in RFC959. It provides wrappers for a subset of the RFC959
28 commands.
29
31 FTP stands for File Transfer Protocol. It is a way of transferring
32 files between networked machines. The protocol defines a client (whose
33 commands are provided by this module) and a server (not implemented in
34 this module). Communication is always initiated by the client, and the
35 server responds with a message and a status code (and sometimes with
36 data).
37
38 The FTP protocol allows files to be sent to or fetched from the server.
39 Each transfer involves a local file (on the client) and a remote file
40 (on the server). In this module, the same file name will be used for
41 both local and remote if only one is specified. This means that
42 transferring remote file "/path/to/file" will try to put that file in
43 "/path/to/file" locally, unless you specify a local file name.
44
45 The protocol also defines several standard translations which the file
46 can undergo during transfer. These are ASCII, EBCDIC, binary, and
47 byte. ASCII is the default type, and indicates that the sender of
48 files will translate the ends of lines to a standard representation
49 which the receiver will then translate back into their local
50 representation. EBCDIC indicates the file being transferred is in
51 EBCDIC format. Binary (also known as image) format sends the data as a
52 contiguous bit stream. Byte format transfers the data as bytes, the
53 values of which remain the same regardless of differences in byte size
54 between the two machines (in theory - in practice you should only use
55 this if you really know what you're doing).
56
58 new ([ HOST ] [, OPTIONS ])
59 This is the constructor for a new Net::FTP object. "HOST" is the
60 name of the remote host to which an FTP connection is required.
61
62 "HOST" is optional. If "HOST" is not given then it may instead be
63 passed as the "Host" option described below.
64
65 "OPTIONS" are passed in a hash like fashion, using key and value
66 pairs. Possible options are:
67
68 Host - FTP host to connect to. It may be a single scalar, as
69 defined for the "PeerAddr" option in IO::Socket::INET, or a
70 reference to an array with hosts to try in turn. The "host" method
71 will return the value which was used to connect to the host.
72
73 Firewall - The name of a machine which acts as an FTP firewall.
74 This can be overridden by an environment variable "FTP_FIREWALL".
75 If specified, and the given host cannot be directly connected to,
76 then the connection is made to the firewall machine and the string
77 @hostname is appended to the login identifier. This kind of setup
78 is also referred to as an ftp proxy.
79
80 FirewallType - The type of firewall running on the machine
81 indicated by Firewall. This can be overridden by an environment
82 variable "FTP_FIREWALL_TYPE". For a list of permissible types, see
83 the description of ftp_firewall_type in Net::Config.
84
85 BlockSize - This is the block size that Net::FTP will use when
86 doing transfers. (defaults to 10240)
87
88 Port - The port number to connect to on the remote machine for the
89 FTP connection
90
91 Timeout - Set a timeout value (defaults to 120)
92
93 Debug - debug level (see the debug method in Net::Cmd)
94
95 Passive - If set to a non-zero value then all data transfers will
96 be done using passive mode. If set to zero then data transfers will
97 be done using active mode. If the machine is connected to the
98 Internet directly, both passive and active mode should work equally
99 well. Behind most firewall and NAT configurations passive mode has
100 a better chance of working. However, in some rare firewall
101 configurations, active mode actually works when passive mode
102 doesn't. Some really old FTP servers might not implement passive
103 transfers. If not specified, then the transfer mode is set by the
104 environment variable "FTP_PASSIVE" or if that one is not set by the
105 settings done by the libnetcfg utility. If none of these apply
106 then passive mode is used.
107
108 Hash - If given a reference to a file handle (e.g., "\*STDERR"),
109 print hash marks (#) on that filehandle every 1024 bytes. This
110 simply invokes the "hash()" method for you, so that hash marks are
111 displayed for all transfers. You can, of course, call "hash()"
112 explicitly whenever you'd like.
113
114 LocalAddr - Local address to use for all socket connections, this
115 argument will be passed to IO::Socket::INET
116
117 If the constructor fails undef will be returned and an error
118 message will be in $@
119
121 Unless otherwise stated all methods return either a true or false
122 value, with true meaning that the operation was a success. When a
123 method states that it returns a value, failure will be returned as
124 undef or an empty list.
125
126 login ([LOGIN [,PASSWORD [, ACCOUNT] ] ])
127 Log into the remote FTP server with the given login information. If
128 no arguments are given then the "Net::FTP" uses the "Net::Netrc"
129 package to lookup the login information for the connected host. If
130 no information is found then a login of anonymous is used. If no
131 password is given and the login is anonymous then anonymous@ will
132 be used for password.
133
134 If the connection is via a firewall then the "authorize" method
135 will be called with no arguments.
136
137 authorize ( [AUTH [, RESP]])
138 This is a protocol used by some firewall ftp proxies. It is used to
139 authorise the user to send data out. If both arguments are not
140 specified then "authorize" uses "Net::Netrc" to do a lookup.
141
142 site (ARGS)
143 Send a SITE command to the remote server and wait for a response.
144
145 Returns most significant digit of the response code.
146
147 ascii
148 Transfer file in ASCII. CRLF translation will be done if required
149
150 binary
151 Transfer file in binary mode. No transformation will be done.
152
153 Hint: If both server and client machines use the same line ending
154 for text files, then it will be faster to transfer all files in
155 binary mode.
156
157 rename ( OLDNAME, NEWNAME )
158 Rename a file on the remote FTP server from "OLDNAME" to "NEWNAME".
159 This is done by sending the RNFR and RNTO commands.
160
161 delete ( FILENAME )
162 Send a request to the server to delete "FILENAME".
163
164 cwd ( [ DIR ] )
165 Attempt to change directory to the directory given in $dir. If
166 $dir is "..", the FTP "CDUP" command is used to attempt to move up
167 one directory. If no directory is given then an attempt is made to
168 change the directory to the root directory.
169
170 cdup ()
171 Change directory to the parent of the current directory.
172
173 pwd ()
174 Returns the full pathname of the current directory.
175
176 restart ( WHERE )
177 Set the byte offset at which to begin the next data transfer.
178 Net::FTP simply records this value and uses it when during the next
179 data transfer. For this reason this method will not return an
180 error, but setting it may cause a subsequent data transfer to fail.
181
182 rmdir ( DIR [, RECURSE ])
183 Remove the directory with the name "DIR". If "RECURSE" is true then
184 "rmdir" will attempt to delete everything inside the directory.
185
186 mkdir ( DIR [, RECURSE ])
187 Create a new directory with the name "DIR". If "RECURSE" is true
188 then "mkdir" will attempt to create all the directories in the
189 given path.
190
191 Returns the full pathname to the new directory.
192
193 alloc ( SIZE [, RECORD_SIZE] )
194 The alloc command allows you to give the ftp server a hint about
195 the size of the file about to be transferred using the ALLO ftp
196 command. Some storage systems use this to make intelligent
197 decisions about how to store the file. The "SIZE" argument
198 represents the size of the file in bytes. The "RECORD_SIZE"
199 argument indicates a maximum record or page size for files sent
200 with a record or page structure.
201
202 The size of the file will be determined, and sent to the server
203 automatically for normal files so that this method need only be
204 called if you are transferring data from a socket, named pipe, or
205 other stream not associated with a normal file.
206
207 ls ( [ DIR ] )
208 Get a directory listing of "DIR", or the current directory.
209
210 In an array context, returns a list of lines returned from the
211 server. In a scalar context, returns a reference to a list.
212
213 dir ( [ DIR ] )
214 Get a directory listing of "DIR", or the current directory in long
215 format.
216
217 In an array context, returns a list of lines returned from the
218 server. In a scalar context, returns a reference to a list.
219
220 get ( REMOTE_FILE [, LOCAL_FILE [, WHERE]] )
221 Get "REMOTE_FILE" from the server and store locally. "LOCAL_FILE"
222 may be a filename or a filehandle. If not specified, the file will
223 be stored in the current directory with the same leafname as the
224 remote file.
225
226 If "WHERE" is given then the first "WHERE" bytes of the file will
227 not be transferred, and the remaining bytes will be appended to the
228 local file if it already exists.
229
230 Returns "LOCAL_FILE", or the generated local file name if
231 "LOCAL_FILE" is not given. If an error was encountered undef is
232 returned.
233
234 put ( LOCAL_FILE [, REMOTE_FILE ] )
235 Put a file on the remote server. "LOCAL_FILE" may be a name or a
236 filehandle. If "LOCAL_FILE" is a filehandle then "REMOTE_FILE"
237 must be specified. If "REMOTE_FILE" is not specified then the file
238 will be stored in the current directory with the same leafname as
239 "LOCAL_FILE".
240
241 Returns "REMOTE_FILE", or the generated remote filename if
242 "REMOTE_FILE" is not given.
243
244 NOTE: If for some reason the transfer does not complete and an
245 error is returned then the contents that had been transferred will
246 not be remove automatically.
247
248 put_unique ( LOCAL_FILE [, REMOTE_FILE ] )
249 Same as put but uses the "STOU" command.
250
251 Returns the name of the file on the server.
252
253 append ( LOCAL_FILE [, REMOTE_FILE ] )
254 Same as put but appends to the file on the remote server.
255
256 Returns "REMOTE_FILE", or the generated remote filename if
257 "REMOTE_FILE" is not given.
258
259 unique_name ()
260 Returns the name of the last file stored on the server using the
261 "STOU" command.
262
263 mdtm ( FILE )
264 Returns the modification time of the given file
265
266 size ( FILE )
267 Returns the size in bytes for the given file as stored on the
268 remote server.
269
270 NOTE: The size reported is the size of the stored file on the
271 remote server. If the file is subsequently transferred from the
272 server in ASCII mode and the remote server and local machine have
273 different ideas about "End Of Line" then the size of file on the
274 local machine after transfer may be different.
275
276 supported ( CMD )
277 Returns TRUE if the remote server supports the given command.
278
279 hash ( [FILEHANDLE_GLOB_REF],[ BYTES_PER_HASH_MARK] )
280 Called without parameters, or with the first argument false, hash
281 marks are suppressed. If the first argument is true but not a
282 reference to a file handle glob, then \*STDERR is used. The second
283 argument is the number of bytes per hash mark printed, and defaults
284 to 1024. In all cases the return value is a reference to an array
285 of two: the filehandle glob reference and the bytes per hash mark.
286
287 feature ( NAME )
288 Determine if the server supports the specified feature. The return
289 value is a list of lines the server responded with to describe the
290 options that it supports for the given feature. If the feature is
291 unsupported then the empty list is returned.
292
293 if ($ftp->feature( 'MDTM' )) {
294 # Do something
295 }
296
297 if (grep { /\bTLS\b/ } $ftp->feature('AUTH')) {
298 # Server supports TLS
299 }
300
301 The following methods can return different results depending on how
302 they are called. If the user explicitly calls either of the "pasv" or
303 "port" methods then these methods will return a true or false value. If
304 the user does not call either of these methods then the result will be
305 a reference to a "Net::FTP::dataconn" based object.
306
307 nlst ( [ DIR ] )
308 Send an "NLST" command to the server, with an optional parameter.
309
310 list ( [ DIR ] )
311 Same as "nlst" but using the "LIST" command
312
313 retr ( FILE )
314 Begin the retrieval of a file called "FILE" from the remote server.
315
316 stor ( FILE )
317 Tell the server that you wish to store a file. "FILE" is the name
318 of the new file that should be created.
319
320 stou ( FILE )
321 Same as "stor" but using the "STOU" command. The name of the unique
322 file which was created on the server will be available via the
323 "unique_name" method after the data connection has been closed.
324
325 appe ( FILE )
326 Tell the server that we want to append some data to the end of a
327 file called "FILE". If this file does not exist then create it.
328
329 If for some reason you want to have complete control over the data
330 connection, this includes generating it and calling the "response"
331 method when required, then the user can use these methods to do so.
332
333 However calling these methods only affects the use of the methods above
334 that can return a data connection. They have no effect on methods
335 "get", "put", "put_unique" and those that do not require data
336 connections.
337
338 port ( [ PORT ] )
339 Send a "PORT" command to the server. If "PORT" is specified then it
340 is sent to the server. If not, then a listen socket is created and
341 the correct information sent to the server.
342
343 pasv ()
344 Tell the server to go into passive mode. Returns the text that
345 represents the port on which the server is listening, this text is
346 in a suitable form to sent to another ftp server using the "port"
347 method.
348
349 The following methods can be used to transfer files between two remote
350 servers, providing that these two servers can connect directly to each
351 other.
352
353 pasv_xfer ( SRC_FILE, DEST_SERVER [, DEST_FILE ] )
354 This method will do a file transfer between two remote ftp servers.
355 If "DEST_FILE" is omitted then the leaf name of "SRC_FILE" will be
356 used.
357
358 pasv_xfer_unique ( SRC_FILE, DEST_SERVER [, DEST_FILE ] )
359 Like "pasv_xfer" but the file is stored on the remote server using
360 the STOU command.
361
362 pasv_wait ( NON_PASV_SERVER )
363 This method can be used to wait for a transfer to complete between
364 a passive server and a non-passive server. The method should be
365 called on the passive server with the "Net::FTP" object for the
366 non-passive server passed as an argument.
367
368 abort ()
369 Abort the current data transfer.
370
371 quit ()
372 Send the QUIT command to the remote FTP server and close the socket
373 connection.
374
375 Methods for the adventurous
376 "Net::FTP" inherits from "Net::Cmd" so methods defined in "Net::Cmd"
377 may be used to send commands to the remote FTP server.
378
379 quot (CMD [,ARGS])
380 Send a command, that Net::FTP does not directly support, to the
381 remote server and wait for a response.
382
383 Returns most significant digit of the response code.
384
385 WARNING This call should only be used on commands that do not
386 require data connections. Misuse of this method can hang the
387 connection.
388
390 Some of the methods defined in "Net::FTP" return an object which will
391 be derived from this class.The dataconn class itself is derived from
392 the "IO::Socket::INET" class, so any normal IO operations can be
393 performed. However the following methods are defined in the dataconn
394 class and IO should be performed using these.
395
396 read ( BUFFER, SIZE [, TIMEOUT ] )
397 Read "SIZE" bytes of data from the server and place it into
398 "BUFFER", also performing any <CRLF> translation necessary.
399 "TIMEOUT" is optional, if not given, the timeout value from the
400 command connection will be used.
401
402 Returns the number of bytes read before any <CRLF> translation.
403
404 write ( BUFFER, SIZE [, TIMEOUT ] )
405 Write "SIZE" bytes of data from "BUFFER" to the server, also
406 performing any <CRLF> translation necessary. "TIMEOUT" is optional,
407 if not given, the timeout value from the command connection will be
408 used.
409
410 Returns the number of bytes written before any <CRLF> translation.
411
412 bytes_read ()
413 Returns the number of bytes read so far.
414
415 abort ()
416 Abort the current data transfer.
417
418 close ()
419 Close the data connection and get a response from the FTP server.
420 Returns true if the connection was closed successfully and the
421 first digit of the response from the server was a '2'.
422
424 The following RFC959 commands have not been implemented:
425
426 SMNT
427 Mount a different file system structure without changing login or
428 accounting information.
429
430 HELP
431 Ask the server for "helpful information" (that's what the RFC says)
432 on the commands it accepts.
433
434 MODE
435 Specifies transfer mode (stream, block or compressed) for file to
436 be transferred.
437
438 SYST
439 Request remote server system identification.
440
441 STAT
442 Request remote server status.
443
444 STRU
445 Specifies file structure for file to be transferred.
446
447 REIN
448 Reinitialize the connection, flushing all I/O and account
449 information.
450
452 When reporting bugs/problems please include as much information as
453 possible. It may be difficult for me to reproduce the problem as
454 almost every setup is different.
455
456 A small script which yields the problem will probably be of help. It
457 would also be useful if this script was run with the extra options
458 "Debug =" 1> passed to the constructor, and the output sent with the
459 bug report. If you cannot include a small script then please include a
460 Debug trace from a run of your program which does yield the problem.
461
463 Graham Barr <gbarr@pobox.com>
464
466 Net::Netrc Net::Cmd
467
468 ftp(1), ftpd(8), RFC 959
469 http://www.cis.ohio-state.edu/htbin/rfc/rfc959.html
470
472 For an example of the use of Net::FTP see
473
474 http://www.csh.rit.edu/~adam/Progs/
475 "autoftp" is a program that can retrieve, send, or list files via
476 the FTP protocol in a non-interactive manner.
477
479 Henry Gabryjelski <henryg@WPI.EDU> - for the suggestion of creating
480 directories recursively.
481
482 Nathan Torkington <gnat@frii.com> - for some input on the
483 documentation.
484
485 Roderick Schertler <roderick@gate.net> - for various inputs
486
488 Copyright (c) 1995-2004 Graham Barr. All rights reserved. This program
489 is free software; you can redistribute it and/or modify it under the
490 same terms as Perl itself.
491
492
493
494perl v5.16.3 2013-02-26 Net::FTP(3pm)