u

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

· 2015-05-06 (수) 23:22:38 · 4611 · 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건
+
제목 글쓴이 날짜 조회
15-08-24 조회 3,810
15-08-23 조회 3,123
15-08-21 조회 2,617
15-08-21 조회 2,551
15-08-20 조회 2,760
15-08-20 조회 3,337
15-08-20 조회 2,744
15-08-13 조회 3,042
15-08-13 조회 3,079
15-08-13 조회 2,616
15-08-12 조회 2,945
15-08-11 조회 2,489
15-08-11 조회 2,775
15-08-10 조회 2,639
15-08-06 조회 3,007
15-08-06 조회 3,359
15-08-03 조회 3,108
15-07-31 조회 2,898
15-07-30 조회 3,292
15-07-29 조회 2,931
15-07-29 조회 2,729
15-07-27 조회 3,316
15-07-21 조회 2,762
15-07-19 조회 3,072
15-07-15 조회 2,688
15-07-14 조회 2,495
15-07-12 조회 3,958
15-07-12 조회 3,030
15-07-12 조회 3,488
15-07-10 조회 2,862
15-07-09 조회 3,301
15-07-09 조회 3,146
15-07-09 조회 2,847
15-07-09 조회 3,264
15-07-08 조회 3,765
15-07-05 조회 3,096
15-07-03 조회 3,124
15-07-02 조회 3,068
15-07-02 조회 2,955
15-06-30 조회 3,886
15-06-26 조회 3,586
15-06-25 조회 2,574
15-06-25 조회 3,052
15-06-24 조회 2,529
15-06-23 조회 3,021
15-06-22 조회 4,745
15-06-22 조회 3,016
15-06-21 조회 5,028
15-06-20 조회 3,655
15-06-19 조회 3,223
15-06-19 조회 2,672
15-06-18 조회 2,932
15-06-18 조회 3,466
15-06-18 조회 4,011
15-06-17 조회 5,065
15-06-17 조회 4,079
15-06-16 조회 4,235
15-06-16 조회 4,793
15-06-16 조회 6,356
15-06-13 조회 3,813
15-06-12 조회 3,152
15-06-10 조회 4,677
15-06-10 조회 2,960
15-06-09 조회 3,205
15-06-09 조회 3,987
15-06-09 조회 2,975
15-06-09 조회 3,075
15-06-08 조회 5,404
15-06-08 조회 2,928
15-06-05 조회 2,752
15-06-05 조회 2,853
15-06-04 조회 2,651
15-06-03 조회 3,293
15-06-01 조회 2,973
15-06-01 조회 2,919
15-06-01 조회 3,013
15-05-31 조회 3,427
15-05-29 조회 4,385
15-05-29 조회 3,443
15-05-29 조회 3,701
15-05-27 조회 4,280
15-05-27 조회 3,792
15-05-26 조회 3,386
15-05-23 조회 3,280
15-05-22 조회 6,206
15-05-20 조회 2,966
15-05-19 조회 3,498
15-05-18 조회 4,351
15-05-18 조회 3,880
15-05-18 조회 5,319
15-05-15 조회 3,783
15-05-15 조회 3,461
15-05-14 조회 3,606
15-05-11 조회 7,388
15-05-10 조회 3,636
15-05-07 조회 4,984
15-05-07 조회 4,342
15-05-06 조회 4,989
15-05-06 조회 4,612
15-05-06 조회 5,393