<?php
function get_remote_filesize($url) {
$headers = get_headers($url, 1);
if (isset($headers["Content-Length"])) {
return is_array($headers["Content-Length"]) ? end($headers["Content-Length"]) : $headers["Content-Length"];
}
return false;
}
function download_file($url, $local_path) {
$file_data = file_get_contents($url);
if ($file_data !== false) {
file_put_contents($local_path, $file_data);
echo "파일을 다운로드했습니다: {$local_path}n";
} else {
echo "다운로드 실패: {$url}n";
}
}
function check_and_download($url, $local_path) {
$remote_size = get_remote_filesize($url);
if ($remote_size === false) {
echo "외부 파일 크기를 확인할 수 없습니다.n";
return;
}
if (file_exists($local_path)) {
$local_size = filesize($local_path);
if ($remote_size == $local_size) {
echo "파일이 이미 최신 상태입니다.n";
return;
}
}
// 파일 크기가 다르면 다운로드
download_file($url, $local_path);
}
// ???? 사용 예제
$url = "//www.example.com/sample.jpg"; // 외부 파일 URL
$local_path = "sample.jpg"; // 저장할 로컬 파일 경로
check_and_download($url, $local_path);
?>