What security considerations should be taken into account when implementing a PHP script to compress and transfer directories between servers?

When implementing a PHP script to compress and transfer directories between servers, security considerations should include validating user input to prevent directory traversal attacks, sanitizing file paths to avoid code injection, and using secure transfer protocols like SSH or HTTPS to protect data in transit.

<?php

// Validate user input to prevent directory traversal attacks
$source_dir = '/path/to/source/dir';
$destination_dir = '/path/to/destination/dir';

if (strpos($source_dir, '..') !== false || strpos($destination_dir, '..') !== false) {
    die('Invalid directory path');
}

// Sanitize file paths to avoid code injection
$source_dir = escapeshellarg($source_dir);
$destination_dir = escapeshellarg($destination_dir);

// Use secure transfer protocols like SSH or HTTPS for data transfer
// Example using SSH with scp
$command = "scp -r $source_dir user@remotehost:$destination_dir";
exec($command);

?>