Current Location: Home> Latest Articles> ftp_rawlist + PHP Cron implements automatic backup detection

ftp_rawlist + PHP Cron implements automatic backup detection

gitbox 2025-05-26

In daily server maintenance, regular backups to remote FTP servers are a common operation. However, relying solely on uploading commands does not ensure that the backup file exists or is uploaded successfully. Therefore, we can combine PHP's ftp_rawlist function and the system planning task (Cron) mechanism to realize the automatic detection of backup files on FTP.

This article will introduce how to use PHP code to list FTP directory files and combine Cron to perform detection tasks periodically to promptly send reminders when files are missing or abnormal.

1. Preparation

Before you start writing your code, make sure the following points are:

  1. There is an accessible FTP server;

  2. The PHP environment supports ftp_* series of functions ( the ftp extension needs to be enabled);

  3. Ability to configure Cron timing tasks under Linux;

  4. You can receive notifications (such as email, Webhook, DingTalk, etc.).

2. PHP script example

The following is a PHP script to detect whether the backup file exists on FTP:

<code> <?php

// FTP configuration
$ftp_host = 'ftp.gitbox.net';
$ftp_user = 'ftp_user';
$ftp_pass = 'ftp_password';
$ftp_dir = '/backups/';
$expected_file = 'backup_' . date('Ymd') . '.zip';

// Create a connection
$conn = ftp_connect($ftp_host);
if (!$conn) {
exit("Cannot connect to FTP server\n");
}

// Log in
if (!ftp_login($conn, $ftp_user, $ftp_pass)) {
ftp_close($conn);
exit("FTP login failed\n");
}

// Switch directory
if (!ftp_chdir($conn, $ftp_dir)) {
ftp_close($conn);
exit("Cannot enter the target directory\n");
}

// Get the file list
$raw_list = ftp_rawlist($conn, ".");
$found = false;

foreach ($raw_list as $line) {
$parts = preg_split("/\s+/", $line);
$filename = end($parts);
if ($filename === $expected_file) {
$found = true;
break;
}
}

ftp_close($conn);

// Output results or send notifications
if ($found) {
echo "file $expected_file exists\n";
} else {
// mail or webhook notifications can be integrated here
echo "Warning: File $expected_file not found\n";
// file_get_contents(" https://gitbox.net/notify?msg=backup_missing&file=$expected_file ");
}
</code>

illustrate:

  • ftp_rawlist() returns a file information list similar to UNIX ls -l , and the file name needs to be resolved manually;

  • date('Ymd') is used to dynamically generate the backup file name of the day;

  • You can access a custom alarm system in the else branch, such as calling a Web API such as https://gitbox.net/notify .

3. Set up Cron timing tasks

Suppose we want to automatically detect backup files at 3 a.m. every day, add the Cron task using the following command:

 crontab -e

Then add:

 0 3 * * * /usr/bin/php /path/to/backup_checker.php >> /var/log/backup_check.log 2>&1

Make sure the /path/to/backup_checker.php script path is correct and has execution permissions.

4. Advanced optimization suggestions

  • Logging : Write detection results to log files for easy tracking;

  • Multi-FTP support : can be encapsulated as functions to support multiple FTP sources;

  • Exception handling : add retry mechanism and detailed error log;

  • Security : Avoid saving passwords plaintext in scripts, consider using .env files or system variables.

5. Summary

Through PHP's ftp_rawlist function combined with Cron timing tasks, we can efficiently implement the automatic detection mechanism of FTP backup files. This solution is lightweight and flexible, suitable for rapid deployment of small and medium-sized projects. For systems with high backup reliability requirements, such automated detection methods are an indispensable guarantee.