Web20 University

How to use PHP to Upload a File using FTP

Last edited on
Datacamp Promotion

* Web 2.0 University is supported by it's audience. If you purchase through links on our site, we may earn an affiliate commision.

There are a few ways to upload files with PHP and probably the most common is by the user selecting a file in an HTML form and then a backend PHP script handling the form data.

A slightly different scenario is to upload files with FTP within PHP code.

In this tutorial, we’ll guide you through the process of connecting to an FTP server and uploading a file using PHP. Additionally, we’ll cover practical use cases and discuss how to handle FTP credentials securely to keep your application safe.


Why Upload Files via FTP Using PHP?

There are several scenarios where uploading files via FTP through PHP is beneficial:

  1. Automated File Management: Automatically upload website backups, logs, or other files to a remote server.
  2. Content Updates: Seamlessly update website content, such as images or documents, without requiring manual file uploads.
  3. Third-Party Integrations: Integrate your application with systems that require files to be delivered to an FTP server.

Step-by-Step Guide to Upload a File via FTP in PHP

Here’s how to upload a file via FTP in PHP:

1. Establish an FTP Connection

To connect to an FTP server, you’ll use PHP’s built-in ftp_connect and ftp_login functions. Ensure that the php-ftp extension is enabled in your PHP installation.

<?php
// FTP server credentials
$ftp_server = "your-ftp-server.com";
$ftp_user = "your-username";
$ftp_pass = "your-password";

// Establish an FTP connection
$conn_id = ftp_connect($ftp_server);

if (!$conn_id) {
    die("Could not connect to FTP server.");
}

// Login to FTP server
if (!ftp_login($conn_id, $ftp_user, $ftp_pass)) {
    die("Could not log in to FTP server.");
}

echo "Connected and logged in successfully!";
?>

2. Upload the File

Use the ftp_put function to upload a file to the server. This function requires the FTP connection, the destination path, and the local file path.

<?php
// Local file path and destination path
$local_file = "path/to/local/file.txt";
$remote_file = "path/on/server/file.txt";

// Upload the file
if (ftp_put($conn_id, $remote_file, $local_file, FTP_BINARY)) {
    echo "File uploaded successfully!";
} else {
    echo "Error uploading file.";
}

// Close the connection
ftp_close($conn_id);
?>

3. Store and Use FTP Credentials Securely

Never hardcode sensitive information, such as FTP credentials, directly in your code. Instead:

  • Use environment variables to store credentials securely.
  • Use a configuration file that’s outside the web root and restrict its permissions.

Example using environment variables:

<?php
$ftp_server = getenv('FTP_SERVER');
$ftp_user = getenv('FTP_USER');
$ftp_pass = getenv('FTP_PASS');

// Establish a connection
$conn_id = ftp_connect($ftp_server);
if (!$conn_id) die("Could not connect to FTP server.");

if (!ftp_login($conn_id, $ftp_user, $ftp_pass)) die("Could not log in.");
?>

Conclusion

Using PHP to upload files via FTP is a powerful way to manage remote servers programmatically. By following this guide, you can establish a secure connection, upload files efficiently, and protect sensitive information.


Additional Resources

Get up to 65% Off Hosting with FREE SSL & FREE Domains!