u

외부이미지 서버에 저장하기

· 2015-05-06 (수) 23:22:38 · 4651 · 1
스크린샷 2015-05-06 오후 11.22.51.png

데모 

 

<?php

//ver1.0 150409 @_untitle_d

 

function save_remote_image($url, $save_path)

{

    $ch = curl_init ($url);

    curl_setopt($ch, CURLOPT_HEADER, 0); // required

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); // required for images

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // maybe redirect on other side?

    curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3'); // or user agent checks?

    $data=curl_exec($ch);

    curl_close ($ch);

$chars_array = array_merge(range(0,9), range('a','z'), range('A','Z'));

//아래의 문자열이 들어간 파일은 -x 를 붙여서 웹경로를 알더라도 실행을 하지 못하도록 함

$filename = preg_replace("/\.(php|phtm|htm|cgi|pl|exe|jsp|asp|inc)/i", "$0-x", basename($url));

shuffle($chars_array);

$shuffle = implode("", $chars_array);

//첨부파일 첨부시 첨부파일명에 공백이 포함되어 있으면 일부 PC에서 보이지 않거나 다운로드 되지 않는 현상이 있습니다. (길상여의 님 090925)

$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;

}

 

$url = 'http://i.imgur.com/OJws8NX.png'; //외부이미지

$save_path = $_SERVER[DOCUMENT_ROOT].'/data/remote/'; 타겟폴더

$remote_image = 'http://'.$_SERVER[SERVER_NAME].'/data/remote/'.save_remote_image($url, $save_path);

?>

 

<img src="<?=$remote_image?>"><br>

<?=$remote_image?>

 

 

 

 

 

관련링크

http://untitled.untitle311apyxry1w48.com/pjtecuh

 

 

 

참고

http://sir.co.kr/

http://stackoverflow.com/questions/7375021/php-retrieve-and-save-remote-image

http://phpschool.com/gnuboard4/bbs/board.php?bo_table=qna_function&wr_id=292925 

|

댓글 1개

좋은정보 감사합니다. ^^
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

8,188건
+
제목 글쓴이 날짜 조회
16-06-03 조회 2,903
15-07-30 조회 3,346
15-06-08 조회 5,502
15-05-27 조회 4,350
15-01-08 조회 6,633
14-12-30 조회 5,357
14-10-10 조회 5,343
14-08-13 조회 6,279
14-08-11 조회 5,115
14-08-06 조회 6,451
14-03-19 조회 7,365
13-04-26 조회 9,531
12-12-15 조회 5,068
12-12-15 조회 7,299
12-12-11 조회 5,414
12-11-21 조회 5,912
12-10-11 조회 5,020
12-10-03 조회 4,713
12-09-20 조회 6,036
12-09-02 조회 4,182
12-08-29 조회 5,158
12-08-29 조회 4,061
12-08-24 조회 4,165
12-08-18 조회 4,398
12-08-17 조회 4,440
12-08-16 조회 3,680
12-08-06 조회 5,465
12-08-05 조회 4,757
12-08-03 조회 4,480
12-08-03 조회 4,626
12-07-29 조회 3,462
12-07-24 조회 4,052
12-07-22 조회 3,543
12-07-20 조회 4,251
12-07-19 조회 4,637
12-07-19 조회 3,875
12-07-19 조회 3,822
12-07-19 조회 5,493
12-07-19 조회 3,434
12-07-18 조회 5,111
12-07-15 조회 4,048
12-07-14 조회 3,743
12-07-12 조회 4,445
12-07-11 조회 3,716
12-07-11 조회 3,432
12-07-11 조회 3,393
12-07-10 조회 4,571
12-07-09 조회 3,308
12-07-09 조회 4,160
12-07-09 조회 4,563
12-07-08 조회 4,480
12-07-07 조회 5,415
12-07-06 조회 4,684
12-07-06 조회 3,952
12-07-06 조회 3,830
12-07-06 조회 3,721
12-07-02 조회 5,428
12-07-02 조회 4,127
12-06-29 조회 3,647
12-06-28 조회 4,269
12-06-26 조회 3,763
12-06-26 조회 4,878
12-06-21 조회 3,850
12-06-20 조회 4,543
12-06-20 조회 4,171
12-06-15 조회 4,523
12-06-15 조회 4,283
12-06-13 조회 7,291
12-06-05 조회 7,271
12-06-04 조회 3,859
12-06-03 조회 3,707
12-05-31 조회 3,715
12-05-28 조회 6,543
12-05-27 조회 3,790
12-05-25 조회 3,933
12-05-23 조회 4,317
12-05-21 조회 5,039
12-05-17 조회 3,992
12-05-17 조회 3,911
12-05-09 조회 3,970
12-05-07 조회 4,217
12-05-02 조회 4,440
12-05-01 조회 4,598
12-04-30 조회 3,345
12-04-30 조회 4,296
12-04-25 조회 4,367
12-04-25 조회 4,802
12-04-24 조회 3,774
12-04-24 조회 4,531
12-04-23 조회 3,543
12-04-20 조회 3,177
12-04-17 조회 5,374
12-04-16 조회 4,301
12-04-14 조회 3,594
12-04-14 조회 3,629
12-04-14 조회 3,680
12-04-13 조회 3,301
12-04-13 조회 4,485
12-04-13 조회 6,106
12-04-12 조회 3,269