Skip to content

Instantly share code, notes, and snippets.

@serverok
Last active September 7, 2024 10:54
Show Gist options
  • Save serverok/2043ad4e171127387f0eb3d8aaabcc55 to your computer and use it in GitHub Desktop.
Save serverok/2043ad4e171127387f0eb3d8aaabcc55 to your computer and use it in GitHub Desktop.
Server to Server File Transfer with PHP script
<?php
# Author: ServerOK
# Web: https://serverok.in/server-to-server-file-transfer-with-php-script
# URL of the file to be downloaded
$source_url = 'https://example.com/backup.zip';
$filename = basename(parse_url($source_url, PHP_URL_PATH));
$destination_path = __DIR__ . '/' . $filename;
function download($source, $destination) {
$ch = curl_init($source);
$fp = fopen($destination, 'w+');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36');
$success = curl_exec($ch);
if ($success === false) {
echo 'cURL Error: ' . curl_error($ch);
return false;
}
curl_close($ch);
fclose($fp);
return true;
}
$success = download($source_url, $destination_path);
if ($success) {
echo '<p style="color: green; font-weight: bold; font-size: 16px;">File transferred successfully to ' . htmlspecialchars($destination_path) . '</p>';
} else {
echo '<p style="color: red; font-weight: bold; font-size: 16px;">File transfer failed</p>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment