PHP FTP

File Transfer Protocol operations in PHP

🌐 What is PHP FTP?

PHP FTP functions enable file transfers between your server and remote FTP servers. You can upload, download, delete files, and manage directories remotely, making it essential for automated backups and file synchronization tasks.


<?php
// Connect to FTP server
$conn = ftp_connect("ftp.example.com");
ftp_login($conn, "username", "password");
echo "Connected to FTP!";
ftp_close($conn);
?>
                                    

Output:

Connected to FTP!

Key FTP Functions

🔌

ftp_connect()

Connects to FTP server

<?php
$conn = ftp_connect(
  "ftp.example.com");
?>
🔑

ftp_login()

Authenticates FTP user

<?php
ftp_login($conn, 
  "user", "pass");
?>
⬆️

ftp_put()

Uploads files to server

<?php
ftp_put($conn, "remote.txt",
  "local.txt", FTP_ASCII);
?>
⬇️

ftp_get()

Downloads files from server

<?php
ftp_get($conn, "local.txt",
  "remote.txt", FTP_ASCII);
?>

🔹 Connecting to FTP Server

Establish FTP connection using ftp_connect() with server address, then authenticate with ftp_login() using credentials. Always check if connection and login succeed before performing operations. Use ftp_ssl_connect() for secure connections when available.

<?php
// Connect to FTP server
$ftp_server = "ftp.example.com";
$ftp_user = "username";
$ftp_pass = "password";

$conn = ftp_connect($ftp_server);

if($conn) {
    $login = ftp_login($conn, $ftp_user, $ftp_pass);
    
    if($login) {
        echo "Successfully connected to FTP!";
    } else {
        echo "Login failed!";
    }
} else {
    echo "Connection failed!";
}
?>

Output:

Successfully connected to FTP!

🔹 Uploading Files

Upload files to remote FTP server using ftp_put(). Specify local file path, remote destination, and transfer mode (FTP_ASCII for text, FTP_BINARY for images/binaries). Returns true on success, enabling automated file deployment and backup systems.

<?php
$conn = ftp_connect("ftp.example.com");
ftp_login($conn, "username", "password");

// Upload a file
$local_file = "document.txt";
$remote_file = "uploads/document.txt";

if(ftp_put($conn, $remote_file, $local_file, FTP_ASCII)) {
    echo "File uploaded successfully!";
} else {
    echo "Upload failed!";
}

ftp_close($conn);
?>

Output:

File uploaded successfully!

🔹 Downloading Files

Download files from FTP server using ftp_get(). Provide local destination path, remote file path, and transfer mode. Essential for retrieving backups, downloading reports, or synchronizing files from remote servers to your local system.

<?php
$conn = ftp_connect("ftp.example.com");
ftp_login($conn, "username", "password");

// Download a file
$remote_file = "data/report.pdf";
$local_file = "downloads/report.pdf";

if(ftp_get($conn, $local_file, $remote_file, FTP_BINARY)) {
    echo "File downloaded successfully!";
} else {
    echo "Download failed!";
}

ftp_close($conn);
?>

Output:

File downloaded successfully!

🔹 Listing Directory Contents

List files and directories on FTP server using ftp_nlist() for simple names or ftp_rawlist() for detailed information. This helps you browse remote directories, verify uploads, and build file management interfaces for FTP operations.

<?php
$conn = ftp_connect("ftp.example.com");
ftp_login($conn, "username", "password");

// List files in directory
$files = ftp_nlist($conn, "/public_html");

echo "Files on server:\n";
foreach($files as $file) {
    echo "- " . $file . "\n";
}

ftp_close($conn);
?>

Output:

Files on server:
- index.html
- style.css
- script.js
- images

🔹 Creating Directories

Create new directories on FTP server using ftp_mkdir(). Returns the directory name on success or false on failure. Useful for organizing uploaded files, creating dated backup folders, or setting up new project structures remotely.

<?php
$conn = ftp_connect("ftp.example.com");
ftp_login($conn, "username", "password");

// Create a new directory
$new_dir = "/public_html/uploads";

if(ftp_mkdir($conn, $new_dir)) {
    echo "Directory created: " . $new_dir;
} else {
    echo "Failed to create directory!";
}

ftp_close($conn);
?>

Output:

Directory created: /public_html/uploads

🔹 Deleting Files

Remove files from FTP server using ftp_delete(). Provide the full path to the file you want to delete. Returns true on success. Use carefully as deletion is permanent. Always verify file paths before deletion operations.

<?php
$conn = ftp_connect("ftp.example.com");
ftp_login($conn, "username", "password");

// Delete a file
$file_to_delete = "/public_html/old_file.txt";

if(ftp_delete($conn, $file_to_delete)) {
    echo "File deleted successfully!";
} else {
    echo "Failed to delete file!";
}

ftp_close($conn);
?>

Output:

File deleted successfully!

🔹 Renaming Files

Rename or move files on FTP server using ftp_rename(). Provide old and new file paths. This can rename files in place or move them to different directories. Useful for organizing files or implementing versioning systems.

<?php
$conn = ftp_connect("ftp.example.com");
ftp_login($conn, "username", "password");

// Rename a file
$old_name = "/public_html/temp.txt";
$new_name = "/public_html/final.txt";

if(ftp_rename($conn, $old_name, $new_name)) {
    echo "File renamed successfully!";
} else {
    echo "Failed to rename file!";
}

ftp_close($conn);
?>

Output:

File renamed successfully!

🧠 Test Your Knowledge

Which function uploads a file to an FTP server?