我编写了一个 PHP 脚本来登录 FTP 服务器,下载一个包含抑制数据的 csv 文件,并将其与我们当前的抑制列表文件合并。
当我从命令行运行它时,它工作得很好。当我将它设置为每天在 crontab 中运行一次时,它总是说它无法从 FTP 服务器下载文件,然后退出。我已经为脚本正在写入文件的目录提供了 777,所以我无法想象这将是一个权限问题。
其他人遇到过这个问题吗?我缺少一些简单的东西吗?下面是我的 crontab 行和我的脚本代码,如果这有助于给出答案。谢谢!
crontab 条目:
0 8 * * * php /var/www/scripts/ftp-unsubs.php >> /var/www/scripts/logs/ftp-unsubs.log
PHP 脚本:
<?
// get latest suppression file and append to our global suppression file (loader.txt)
$yesterday = date("Y-m-d", strtotime("yesterday"));
$ftp_server = '--hidden--';
$ftp_user_name = '--hidden--';
$ftp_user_pass = '--hidden--';
$local_file = 'unsub_dls/unsubs.' . $yesterday . '.csv';
$remote_file = 'FJM378_unsubs.' . $yesterday . '.csv';
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_pasv($conn_id, TRUE);
print "\n[" . date("r") . "]\n";
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!\n";
echo "Attempted to connect to $ftp_server for user $ftp_user_name\n";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name\n";
}
// try to download $remote_file and save to $local_file
if (ftp_get($conn_id, $local_file, $remote_file, FTP_BINARY)) {
echo "Successfully written to $local_file\n";
} else {
echo "Couldn't get file $remote_file\n";
}
// close the FTP stream
if (ftp_close($conn_id)) {
print "Closed the connection to $ftp_server\n";
}
// grab new unsubs and add timestamp
print "\nProcessing file $local_file\n";
if (file_exists($local_file)) {
$fh = fopen($local_file, 'r');
$i = 0;
$unsub = "";
while (!feof($fh)) {
$line = fgets($fh);
$line = preg_replace('/(?:(?:\r\n|\r|\n)\s*){2}/s', "", $line);
if ($line != "" && $line != "email") {
$unsub .= $line . "," . date("Y-m-d H:i:s") . "\n";
$i++;
}
}
fclose($fh);
// add unsubs to global unsub file
$fsupp = fopen('loader.txt','a');
fwrite($fsupp, $unsub);
fclose($fsupp);
print "Added $i unsubs to loader.txt\n\n";
} else {
print "Error: file '$local_file' doesn't exist! Bailing.\n\n";
}
?>