How to Search and Replace Files on an FTP Server Using PHP
Learn step-by-step how to connect to an FTP server with PHP, list remote files, download them, replace specific strings, upload the modified files back, and properly close the connection, enabling automated search-and-replace operations across your server’s directories.
Preparation
Before starting, ensure you have a PHP environment that can connect to an FTP server and that you possess the FTP host, username, password, and port information.
Connecting to FTP Server
Use PHP's FTP extension to establish a connection, authenticate, and switch to passive binary mode.
// Set FTP server connection info
$ftp_server = 'ftp.example.com';
$ftp_user = 'username';
$ftp_pass = 'password';
$ftp_port = 21;
// Establish FTP connection
$ftp_conn = ftp_connect($ftp_server, $ftp_port);
if (! $ftp_conn) {
die("无法连接到FTP服务器");
}
// Log in to FTP server
if (!ftp_login($ftp_conn, $ftp_user, $ftp_pass)) {
die("FTP登录失败");
}
// Set transfer mode to binary (passive)
ftp_pasv($ftp_conn, true);Search and Replace Files
After connecting, list the files in the target directory, download each file to a temporary local copy, replace the desired string, upload the modified file back, and clean up the temporary file.
// Define strings to search and replace
$old_string = 'example.com';
$new_string = 'example.net';
// Target remote directory
$remote_dir = '/path/to/remote/directory';
// List files in the directory
$file_list = ftp_nlist($ftp_conn, $remote_dir);
// Iterate over each file and replace content
foreach ($file_list as $file) {
// Download file to a temporary local file
$temp_file = tempnam(sys_get_temp_dir(), 'tempfile');
ftp_get($ftp_conn, $temp_file, $file, FTP_BINARY);
// Read file content
$file_content = file_get_contents($temp_file);
// Replace the target string
$new_content = str_replace($old_string, $new_string, $file_content);
// Write the modified content back to the temporary file
file_put_contents($temp_file, $new_content);
ftp_put($ftp_conn, $file, $temp_file, FTP_BINARY);
// Delete the temporary local file
unlink($temp_file);
}
// Close the FTP connection
ftp_close($ftp_conn);Conclusion
The script demonstrates how to programmatically connect to an FTP server using PHP, enumerate files, download them, perform a search-and-replace on their contents, upload the updated files, and finally close the connection, providing a reusable automation pattern for bulk file modifications.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
