In web development and maintenance, FTP servers are often used for uploading, downloading, and managing backups. Over time, these servers can accumulate a large number of outdated files, consuming valuable storage space. To keep your system clean and efficient, it’s important to perform regular file cleanup. This article will show you how to use a PHP script to automate that process on your FTP server.
FTP (File Transfer Protocol) is one of the most common network protocols for transferring files. Through an FTP client or a script, you can upload, download, and delete files on a remote server. To automate file deletion, you first need to connect to the FTP server and retrieve file information using PHP.
PHP provides built-in FTP functions that make it easy to connect and interact with an FTP server. Here’s an example:
$ftpServer = 'ftp.example.com';
$ftpUsername = 'username';
$ftpPassword = 'password';
$connId = ftp_connect($ftpServer);
$loginResult = ftp_login($connId, $ftpUsername, $ftpPassword);
if ($connId && $loginResult) {
// Connection successful
} else {
// Connection failed, handle the error
}In the code above, ftp_connect() establishes the connection and ftp_login() performs the login. Make sure the server address, username, and password are correct before proceeding.
Once connected, you can retrieve a list of files in a specific FTP directory using ftp_nlist():
$ftpDirectory = '/path/to/directory/';
$fileList = ftp_nlist($connId, $ftpDirectory);
foreach ($fileList as $file) {
echo $file . '<br>';
}This code outputs the names of all files in the specified directory, providing the basis for selecting and deleting outdated files.
Next, you can check each file’s modification date and remove files older than a specified number of days. Here’s an example:
$ftpDirectory = '/path/to/directory/';
$daysAgo = 30; // Delete files older than 30 days
$fileList = ftp_nlist($connId, $ftpDirectory);
foreach ($fileList as $file) {
$fileTimestamp = ftp_mdtm($connId, $file);
$fileTimestamp = date_create_from_format('YmdHis', $fileTimestamp);
$currentTimestamp = date_create();
$diff = date_diff($currentTimestamp, $fileTimestamp);
$elapsedDays = $diff->format('%a');
if ($elapsedDays > $daysAgo) {
ftp_delete($connId, $file);
echo 'Deleted file: ' . $file . '<br>';
}
}This script iterates through the directory, checks each file’s last modified date, and automatically deletes those older than the defined time period.
To run the cleanup automatically, you can use a Linux cron job. Open the crontab editor using:
crontab -eThen add the following line:
0 0 * * * php /path/to/script.phpThis command schedules the PHP script to run every day at midnight, automatically deleting outdated files from the FTP server.
In this tutorial, we explored how to use PHP to schedule automatic deletion of old files on an FTP server. We covered connecting to the FTP server, listing files, identifying old files, and setting up cron jobs for automation. This approach can help free up disk space, improve performance, and reduce manual maintenance. For developers managing frequent file uploads or backups, this is a simple and effective solution.