채택완료

외부 이미지 서버 저장 코드 좀 봐주세요

2017-12-04 (월) 15:52:52 2,469
Copy
<?php
//ver1.0 150410 @_untitle_d

function save_remote_image($url, $save_path)
{
    $ch = curl_init ($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $data=curl_exec($ch);
    curl_close ($ch);
 
 $chars_array = array_merge(range(0,9), range('a','z'), range('A','Z'));
 $filename = preg_replace("/\.(php|phtm|htm|cgi|pl|exe|jsp|asp|inc)/i", "$0-x", basename($url));
 shuffle($chars_array);
 $shuffle = implode("", $chars_array);
 $change_filename = abs(ip2long($_SERVER[REMOTE_ADDR])).'_'.substr($shuffle,0,8).'_'.str_replace('%', '', urlencode(str_replace(' ', '_', $filename)));
 $out_path = $save_path.$change_filename;
 
    if(file_exists($out_path)) @unlink($out_path);
    $fp = fopen($out_path,'x');
    fwrite($fp, $data);
    fclose($fp);
 
 return $change_filename;
}


if ($w == '' || $w == 'r'){

 $data_dir = G5_DATA_PATH.'/file/'.$bo_table.'/';
 $data_url = G5_DATA_URL.'/file/'.$bo_table.'/';
 
 $img_content = str_replace("&gt;", ">", str_replace("&lt;", "<", stripslashes($_POST[wr_content])));
 $patten = "/<img[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i";
 preg_match_all($patten, $img_content, $match);
 
 if ($match[1]) {
  foreach ($match[1] as $link) {
   $url = parse_url($link);
   if ($url[host] && $url[host] != $_SERVER[HTTP_HOST]) {
    $img_content = str_replace($link, ($data_url.save_remote_image($link, $data_dir)), $img_content);
   }
  }
  
  $sql = "update ".$write_table." set wr_content = '".sql_real_escape_string(trim($img_content))."' where wr_id = '".$wr_id."'";
  sql_query($sql);
 }
 
}
?>

같은 호스팅 내의 계정을 바꿔서 데이터 이전 후에 이상이 생겼습니다

원래는 잘 작동됐지만 데이터 이전 후에 글쓰기 시에 다른 외부 주소의 이미지를 가져오면

3분 이상의 전체 페이지 로딩 지연과 함께 길어지면 http 500 오류도 떠버립니다

호스팅사에 문의해봐도 별 다른 문제는 없습니다

도대체 뭐가 문제일까요?

|

답변 2개 / 댓글 2개

채택된 답변
+20 포인트
2017-12-04 (월) 16:08:15

외부 이미지 서버가 느린거 같네요.

서버 상황을 봐야 정확한 원인이 나올것 같지만, 위의 코드만 봤을때는, 외부에 있는 이미지 서버가 느리거나, 외부 이미지서버와의 통신구간에 속도 문제가 있을걸로 보입니다.

답변에 대한 댓글 1개

외부 이미지 서버가 느린 건 아닌 것 같습니다
네이버 카페거든요.. 이런 속도는 데이터 이전 후 처음 글 썼을 때 이후로 계속 겪고 있고
다른 이미지 서버도 테스트 해봤지만 동일합니다
문제가 있다면 뭘 고쳐야 하는 건지요?
2017-12-04 (월) 19:15:48

그렇다면, 느린 증상을 자세히 설명해주세요.  네트웍 문제 아니면, disk 문제입니다. 외부 이미지를 로컬에 저장할때 문제일것 같네요. iowait 를 보셔야 할것 같습니다. 특히 disk 쪽

답변에 대한 댓글 1개

말씀해주신 증상과는 전혀 다른 쪽이었네요.. 고쳤습니다

답변을 작성하려면 로그인이 필요합니다.