What are some best practices for renaming files to avoid overwriting existing ones during FTP operations?

When renaming files during FTP operations, it is important to ensure that the new file name does not overwrite any existing files. One way to avoid this issue is to generate a unique file name by appending a timestamp or a random string to the original file name before uploading it. This will help prevent any conflicts with existing files on the server.

// Generate a unique file name by appending a timestamp
$timestamp = time();
$newFileName = $timestamp . '_' . $originalFileName;

// Upload the file using the new unique file name
ftp_put($ftpConnection, $newFileName, $localFilePath, FTP_BINARY);