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

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.
Tutorial
Introduction
Installation
Syntax
Variable
Superglobals
Data Types
String
Array
Multidimensional Array
Sort Array
Constant
Operators
Cookies
Sessions
DateTime
Error Handling
Exception Handling
File
Write and Create File
File Open, Read and Close
File Upload
Filtering
Redirecting
Advanced Filters
Forms
Form Required Field
Validate Email/URL
Form Validation
Form Action
Function
Prepared Statements
JSON
Calendar
ZIP File
FTP
HTTP Response
DateTime Functions
Error Functions
File Function
Filter
Math Functions
Mail Function
Miscellaneous Functions
Date Format
String Functions
Array Functions
Directory Functions
MySQL Database
MySQL Connection
MySQL Create Database
MySQL Create Table
MySQL Delete Data
MySQL Insert Data
MySQL Get Last Record ID
MySQL Insert Multiple Records
MySQL Select Data
MySQL Limit Data
MySQL Update Data
MySQLi Functions
AJAX and MySQL
AJAX Search
AJAX Poll
RSS Reader
Read XML File in PHP
XML Parser
SimpleXML Parser
SimpleXML: Node and Attribute
Expat XML Parser
DOMDocument
Libxml Functions
SimpleXML Functions
XML Parsing Functions
PHP isset
PHP echo and print
PHP if else and elseif
PHP switch case
PHP include File
PHP while Loop
PHP for and foreach
PHP mail()
PHP explode()
PHP substr()
PHP str_replace()
PHP array_push
PHP count()