Other helpful FTP functions

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.

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Curl >>

Previous chapter: Transferring files over FTP

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.