What is the best method to download a remote file to a server using PHP, considering potential issues with empty files?

When downloading a remote file to a server using PHP, it is important to handle the possibility of empty files. One way to address this issue is to check the file size after downloading and ensure it is not empty before saving it to the server.

<?php
$fileUrl = 'http://example.com/remote-file.txt';
$localFilePath = '/path/to/save/file.txt';

// Download the remote file
$fileContent = file_get_contents($fileUrl);

// Check if the file is not empty
if(!empty($fileContent)) {
    // Save the file to the server
    file_put_contents($localFilePath, $fileContent);
    echo 'File downloaded successfully.';
} else {
    echo 'Error: Empty file.';
}
?>