🚨 Time is Running Out: Reserve Your Spot in the Lucky Draw & Claim Rewards! START NOW

Code has been added to clipboard!

PHP FTP: A Handy Guide for Working with FTP Servers

Reading time 4 min
Published Aug 8, 2017
Updated Oct 2, 2019

Internet connection would be useless if we wouldn't be able to share data. In this lesson, we will introduce you to the functions and predefined constants needed to make PHP create directories, reach PHP downloadable files, and perform other tasks during a PHP FTP connection.

All file sharing works by connecting computers with one another. For that, protocols are used. FTP (which stands for File Transfer Protocol) is a client-server protocol. The functions for working with FTP have been around since PHP 3 (some constants have been introduced in version 4.3, though), but not every file server supports them.

PHP FTP: Main Tips

  • PHP FTP functions are meant to allow you to access FTP files servers through PHP script.
  • FTP functions can be used in order to login, close connections, upload, rename, delete, download, get info on the what the file server contains. You can also make PHP create directory for your own use.
  • To use these functions, you must compile PHP with --enable-ftp. Windows PHP version has inbuilt support for this extension.
  • If all you want to do is to read or write to a file in a file server, you can use the wrapper ftp:// along with filesystem functions that are more simple and intuitive.

List of Functions

Take a look at the table below. The functions are listed alphabetically and an explanation for each one is provided. Whenever you need to PHP create directories or PHP download file from server, you can easily choose the function you need from this table:

Function Description
ftp_alloc() Allocate space for file to be uploaded to the FTP server
ftp_cdup() Change to parent directory on FTP server
ftp_chdir() Change current directory on FTP server
ftp_chmod() Set permissions on file via FTP
ftp_close() Close FTP connection
ftp_connect() Open FTP connection
ftp_delete() Delete file on FTP server
ftp_exec() Execute command on FTP server
ftp_fget() PHP download file from server and save into open local file
ftp_fput() Uploads from an open file and saves to file on FTP server
ftp_get_option() Return runtime options of FTP connection
ftp_get() PHP download file from server
ftp_login() Logs in to FTP connection
ftp_mdtm() Return last modified time of specified file
ftp_mkdir() PHP create directory on FTP server
ftp_nb_continue() Continue retrieving/sending file (non-blocking)
ftp_nb_fget() Get PHP downloadable file from server and save into open file (non-blocking)
ftp_nb_fput() Upload from open file and save to file on FTP server (non-blocking)
ftp_nb_get() Get PHP downloadable file from server (non-blocking)
ftp_nb_put() Upload file to FTP server (non-blocking)
ftp_nlist() Return list of files in specified directory on FTP server
ftp_pasv() Turns passive mode on / off
ftp_put() Upload file to FTP server
ftp_pwd() PHP get current directory name
ftp_quit() An alias of ftp_close() function
ftp_raw() Send raw command to FTP server
ftp_rawlist() Return list of files with file information from specified directory
ftp_rename() Rename file or directory on FTP server
ftp_rmdir() Delete empty directory on FTP server
ftp_set_option() Sets runtime options for FTP connection
ftp_site() Send FTP SITE command to FTP server
ftp_size() Return size of specified file
ftp_ssl_connect() Open secure SSL-FTP connection
ftp_systype() Return system type identifier of FTP server
DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
Udemy
Pros
  • Easy to navigate
  • No technical issues
  • Seems to care about its users
Main Features
  • Huge variety of courses
  • 30-day refund policy
  • Free certificates of completion

Revelant Predefined Constants

These constants might also prove useful when working with PHP FTP connections. As you can see, all of them are integers (contain numerical values) and work easily since PHP 4.3 was first introduced:

Constant Type PHP version
FTP_ASCII Int PHP 3 and newer
FTP_TEXT Int PHP 3 and newer
FTP_BINARY Int PHP 3 and newer
FTP_IMAGE Int PHP 3 and newer
FTP_TIMEOUT_SEC Int PHP 3 and newer
FTP_AUTOSEEK Int PHP 4.3 and newer
FTP_AUTORESUME Int PHP 4.3 and newer
FTP_FAILED Int PHP 4.3 and newer
FTP_FINISHED Int PHP 4.3 and newer
FTP_MOREDATA Int PHP 4.3 and newer

PHP FTP: Summary

  • Using PHP FTP functions in your code, you can reach (upload, download, rename, etc.) files kept in FTP servers or PHP create directory from scratch.
  • If you are not using PHP version made for Windows, you will need an extension called --enable-ftp.
  • Wrapper ftp:// and simple filesystem functions will do just fine if all you need done with a particular file is reading or writing into it.
  • Keep in mind you might get different results using these functions on different file servers.