This is untested and has no error-checking, but it should come close:
$local_dir = 'local/dir';
$remote_server = 'example.com';
$remote_dir = 'remote/dir';
$char_arr = array_merge(range('a', 'z'), range('0', '9'));
$name_len = 10;
$dh = dir($local_dir);
while (($file = $dh->read()) !== false) {
if (substr($file, -4) == '.ram') {
$rand_str = '';
for ($i = 0; $i < $name_len; $i++) {
$rand_str .= $char_arr[mt_rand(0, count($char_arr) - 1];
}
$contents = trim(file_get_contents($file));
$old_name = ltrim(strrchr($contents, '/'), '/');
$new_names[$old_name] = $rand_str;
$new_contents = str_replace($old_name, $rand_str . 'rm', $contents);
$fp = fopen($file, 'wb');
fwrite($fp, $new_contents);
fclose($fp);
}
}
$dh->close();
$connection = ftp_connect($remote_server);
$login = ftp_login($connection, $user, $pword);
$remote_files = ftp_nlist($connection, $remote_dir);
foreach ($remote_files as $file) {
if (array_key_exists($file, $new_names)) {
ftp_rename($connection, $file, $new_names[$file] . 'rm');
}
}
ftp_close($connection);