FTP upload with PHP

In this tutorial I will demonstrate how to perform FTP uploads using PHP. PHP offers some functions that make working with FTP very simple. The process we’ll use in this example is to:

  • Connect to the server
  • Login to the server
  • Upload the file
  • Close the connection

If you want to skip the reading you can go straight to a full working example at the bottom of this tutorial.

Establishing An FTP Connection

The first step is to connect to the FTP server. To do this we use the ftp_connect() function, passing the server domain name as a parameter.

$conn_id = ftp_connect('myserver.com');

To ensure our connection was successful we can check the return value.

if (!$conn_id) {
    // connection failed
} else {
    // connection was successful
}

Logging Into The Server

Now that we have a connection to the server we will likely need to authenticate before we can work with files. We login using the ftp_login() function.

$login_result = ftp_login($conn_id, 'myuser', 'mypassword');

As with our connection it is a good idea to check the return value to make sure our credentials were accepted.

if (!$login_result) {
    // login failed
} else {
    // login was successful
}

Uploading The File

Now we perform the actual file upload. We do this using the ftp_put() function. With this function we can upload as text or binary. I prefer binary so it works with all types of files.

// upload the file
$destination = '/public_html/uploads/somefile.txt';
$source = dirname(__FILE__).'/somefile.txt';
$upload = ftp_put($conn_id, $destination, $source, FTP_BINARY);

To check for a successful upload we can check the return value of the function.

if (!$upload) {
    // upload failed
} else {
    // upload was successful
}

Close The FTP Connection

The last step is to clean up by closing our FTP connection. We do this by passing our connection variable into the ftp_close() function.

ftp_close($conn_id);

Complete FTP Example

Here is a complete PHP FTP example you can use to test.

<?php
 
// upload file through FTP using PHP
 
function UploadFile($filename){
 
    // set up basic connection
    echo ("Attempting to connect through FTP...\n");
    $conn_id = ftp_connect('myserver.com');
 
    // check connection
    if (!$conn_id) {
        echo ("Connecting failed!\n");
        exit;
    } else {
        echo ("Established connection successfully.\n");
    }
 
    // login with username and password
    echo ("Logging in...\n");
    $login_result = ftp_login($conn_id, 'myuser', 'mypassword');
 
    // check result
    if (!$login_result) {
        echo ("Login failed!\n");
        exit;
    } else {
        echo ("Logged in successfully.\n");
    }
 
    // upload the file
    echo ("Uploading '$filename'...\n");
    $destination = '/public_html/uploads/'.$filename;
    $source = dirname(__FILE__).'/'.$filename;
    $upload = ftp_put($conn_id, $destination, $source, FTP_BINARY);
 
    // check result
    if ($upload) {
        echo ("Upload was successful.\n");
    } else {
        echo ("Upload failed!\n");
        exit;
    }
 
    // close our connection
    echo ("Closing FTP connection...\n");
    ftp_close($conn_id);
    echo ("Connection closed.\n");
}
 
// upload a couple files.
UploadFile('uploadme.txt');
UploadFile('confetti.jpg');