Hudzilla.org - the homepage of Paul Hudson
Contents > Networks > Transferring files over FTP: ftp_connect(), ftp_login(), ftp_pasv(), ftp_chdir(), ftp_get(), and ftp_close() Wish List | Report Bug | About Me ]

15.9.1     Other helpful FTP functions: ftp_put(), ftp_systype(), ftp_rename(), ftp_mkdir(), and ftp_rmdir()

This is NOT the latest copy of this book; click here for the latest version.

bool ftp_put ( resource ftp_stream, string remote_file, string local_file, int mode [, int start_position])

string ftp_rename ( resource ftp_stream, string from, string to)

string ftp_mkdir ( resource ftp_stream, string directory)

string ftp_rmdir ( resource ftp_stream, string directory)

As you can see, using the FTP functionality inside PHP puts FTP access just a few function calls away, and you have much more flexibility than using fopen(), too. There are several other FTP-related functions that are likely to also be useful, of which the most obvious is ftp_put() - the opposite of ftp_get() - but there is also ftp_systype() and ftp_rename().

While ftp_put() is very close to ftp_get(), it takes its parameters in a slightly different order - first you provide the FTP connection, then the name of the filename on the remote server where you are uploading to, then the name of the local file you want to upload, and finally the transfer type. The actual values are the same as before, but you do need to remember that the local filename and remote filename are swapped.

Ftp_systype() takes the FTP connection resource as its only parameter, and returns a string of the name of the system type. Unix systems will return "Unix", whereas Windows NT-based systems, including much more recent releases of Windows, report Windows_NT. At the time of writing, long after Microsoft deployed Windows Server 2003 on their own machines, their FTP server still reports Windows_NT - not necessarily because that is what they run, but more for legacy reasons.

To rename files on the remote server use ftp_rename(), which takes three parameters: the FTP connection, the file to rename, and the new filename. There is also ftp_mkdir() and ftp_rmdir() to create and remove directories, both of which take the directory name as their only parameter, and both give you more control over the layout of your files.





<< 15.9 Transferring files over FTP: ftp_connect(), ftp_login(), ftp_pasv(), ftp_chdir(), ftp_get(), and ftp_close()   15.10 Curl >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is six plus ten?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow