Uploading Files Using PHP's FTP Functions
From TPC! Web Development
Contents |
Step 1: Define FTP connection variables
First, we need to define our connection variables. Use the same variables you would when using an FTP client (Filezilla, CuteFTP, CoreFTP, etc.):
// FTP server details $ftp_server = 'ftp.server.com'; $ftp_user = 'username'; $ftp_password = 'password';
Step 2: Connect to FTP server
To connect to the FTP server, we use the following code:
$ftp = ftp_connect($ftp_server);
Step 3: Login and check FTP connection
After connecting to the database, we want to start an FTP session:
$ftp_login = ftp_login($ftp, $ftp_user, $ftp_password);
The $ftp variable above references the FTP connection we opened in step two. Next, we need to check to make sure that the connection is valid, and we are properly logged in.
// Check FTP connection and login status if ((!$ftp) || (!$ftp_login)) { exit("Error connecting to <em>$ftp_server</em> as <em>$ftp_user</em>."); } else { echo "Connected to $ftp_server\n"; }
This part is pretty self-explanatory. If there is no FTP connection ($ftp) or FTP session ($ftp_login), we exit the script. In this particular statement, we kill the script, but there does not have to be a fatal (script-ending) error here.
Step 4: Enable passive mode
The ftp_pasv() function toggles passive mode. In passive mode, the data connect is initiated by the client and not the server. It is important to call this function only after a successful login to the FTP server. To enable passive mode:
ftp_pasv($ftp, true);
The $ftp variable references the FTP connection we created during the second step.
Step 5: Upload the file
In step four, we use the ftp_put() function to upload a local file to the FTP server. There are four parameters required for the ftp_put() function.
/** * ftp_put(ftp_stream, remote_file, local_file, mode) * @param ftp_stream The link identifier of the FTP connection * @param remote_file Remote file path (location to place the file on server) * @param local_file Local file path (location of local file to upload) * @param mode FTP_ASCII for text files or FTP_BINARY for binary (images, programs, etc.) */ // Uploads a binary file (images, programs, etc.) if (ftp_put($ftp, $remote_file, $local_file, FTP_BINARY)) { echo "Successfully uploaded $local_file\n"; } else { echo "Error uploading $local_file\n"; } // Uploads a plain text file if (ftp_put($ftp, $remote_file, $local_file, FTP_ASCII)) { echo "Successfully uploaded $local_file\n"; } else { echo "Error uploading $local_file\n"; }
Step 6: Close the connection
Lastly, we close the connection to the FTP server, which wraps up our script:
ftp_close($ftp);
Source code
<?php /** * FTP upload script using PHP's FTP functions * * @package tpc_tutorials * @version 0.1 20090312T202200-05:00 * @author Chris Strosser * @link http://dev.thatspoppycock.com/index.php/Uploading_Files_Using_PHP%27s_FTP_Functions */ // FTP server details $ftp_server = 'ftp.server.com'; $ftp_user = 'username'; $ftp_password = 'password'; // Connect to FTP server $ftp = ftp_connect($ftp_server); // Login and open an FTP session $ftp_result = ftp_login($ftp, $ftp_user, $ftp_password); // Check FTP connection and login status if ((!$ftp) || (!$ftp_result)) { exit("Error connecting to <em>$ftp_server</em> as <em>$ftp_user</em>."); } else { echo "Connected to $ftp_server\n"; } // Enable passive mode ftp_pasv($ftp, true); // Upload a binary file (images, programs, etc.) if (ftp_put($ftp, $remote_file, $local_file, FTP_BINARY)) { echo "Successfully uploaded $local_file\n"; } else { echo "Error uploading $local_file\n"; } // Upload a plain text file if (ftp_put($ftp, $remote_file, $local_file, FTP_ASCII)) { echo "Successfully uploaded $local_file\n"; } else { echo "Error uploading $local_file\n"; } // Close connection ftp_close($ftp); ?>

