PHP

[PHP] 외부이미지 서버 저장 / php curl 이미지 저장

chsr 2021. 3. 30. 11:46
728x90
반응형

■ PHP 외부이미지 서버 저장 / php curl 이미지 저장


■ 사용법

$imgLink = "https://test_imglink.co.kr/test.jpg";

// 파일명 가져오기
$linkArray = explode("/", $imgLink);
$filename = $linkArray[count($linkArray)-1];

// 확장자명 가져오기
//$ext = strtolower(pathinfo($imgLink, PATHINFO_EXTENSION));	// jpg

// 저장할 이미지 위치 및 파일명
$fp = fopen("./upload/".$filename,'w');

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $imgLink);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
curl_close($ch);

// 가져올 외부이미지 주소
fwrite($fp,$contents);
fclose($fp);

echo '<img src="/upload/'.$filename.'">';
728x90
반응형