Are there alternative methods to FTP for accessing server files using PHP?

Alternative methods to FTP for accessing server files using PHP include using SSH2 functions or cURL library. SSH2 functions allow secure file transfer and remote command execution using the SSH protocol. cURL library can be used to transfer files using various protocols like HTTP, HTTPS, FTP, etc. These methods provide more flexibility and security compared to traditional FTP.

// Example using SSH2 functions
$connection = ssh2_connect('example.com', 22);
ssh2_auth_password($connection, 'username', 'password');
$sftp = ssh2_sftp($connection);
$stream = fopen("ssh2.sftp://$sftp/path/to/file.txt", 'r');

// Example using cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'ftp://example.com/path/to/file.txt');
curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);