php
PHP - Curl를 이용해 FTP 제어하기
PHP에는 자체 FTP 함수가 있습니다. 여기서는 FTP 함수를 이용하지 않고 오직 CURL 만을 이용해 FTP 를 관리해 보겠습니다.
CURL에는 많은 기능이 있습니다. 자주 사용하는 몇 가지를 소개합니다.
1. 파일을 업로드하는 방법
2. 파일을 다운로드 방법
3. 파일을 삭제하는 방법
4. 파일의 이름을 변경
5. 폴더를 삭제하는 방법
6. 폴더를 만드는 방법
7. 권한을 설정하는 방법
8. 파일 목록을 출력하는 방법
CURL에는 많은 기능이 있습니다. 자주 사용하는 몇 가지를 소개합니다.
1. 파일을 업로드하는 방법
<?php
// 서버 경로
$realserver = 'ftp://habonyphp.com/public_html/data/';
// 서버 사용자 정보
$user = 'habony';
$pass = '123456';
// 업로드할 파일
$realfile = $_FILES['uerfile']['tmp_name'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, @fopen($realfile, 'r'));
curl_setopt($ch, CURLOPT_INFILESIZE, @filesize($realfile));
curl_setopt($ch, CURLOPT_URL, $realserver);
curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $pass);
curl_exec($ch);
curl_close($ch);
?>
2. 파일을 다운로드 방법
<?php
// 서버 경로
$realserver = 'ftp://habonyphp.com/public_html/data/';
// 서버 사용자 정보
$user = 'habony';
$pass = '123456';
// 다운로드할 파일
$realfile = 'example.gif';
$remote = tempnam(sys_get_temp_dir(), 'CURL_FTP');
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_FILE, $fp = @fopen($remote, 'w+'));
curl_setopt($ch, CURLOPT_URL, $realserver . $realfile);
curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $pass);
curl_exec($ch);
curl_close($ch);
fclose($fp);
// URL 경로
$url = $remote;
?>
3. 파일을 삭제하는 방법
<?php
// 서버 경로
$realserver = 'ftp://habonyphp.com/public_html/data/';
// 서버 사용자 정보
$user = 'habony';
$pass = '123456';
// 삭제할 파일
$list = array(
'DELE habony.gif', 'DELE namefile.bmp', 'DELE example.jpg'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTQUOTE, $list);
curl_setopt($ch, CURLOPT_URL, $realserver);
curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $pass);
curl_exec($ch);
curl_close($ch);
?>
4. 파일의 이름을 변경
<?php
// 서버 경로
$realserver = 'ftp://habonyphp.com/public_html/data/';
// 서버 사용자 정보
$user = 'habony';
$pass = '123456';
// 현재 파일
$old = 'old.bmp';
// 새로운 이름
$new = 'new.bmp';
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTQUOTE, array("RNFR " . $old, "RNTO " . $new);
curl_setopt($ch, CURLOPT_URL, $realserver);
curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $pass);
curl_exec($ch);
curl_close($ch);
?>
5. 폴더를 삭제하는 방법
<?php
// 서버 경로
$realserver = 'ftp://habonyphp.com/public_html/data/';
// 서버 사용자 정보
$user = 'habony';
$pass = '123456';
// 삭제할 폴더
$rmdir = 'dirname';
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTQUOTE, array("RMD " . $rmdir);
curl_setopt($ch, CURLOPT_URL, $realserver);
curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $pass);
curl_exec($ch);
curl_close($ch);
?>
6. 폴더를 만드는 방법
<?php
// 서버 경로
$realserver = 'ftp://habonyphp.com/public_html/data/';
// 서버 사용자 정보
$user = 'habony';
$pass = '123456';
// 생성할 폴더
$mkdir = 'dirname';
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTQUOTE, array("MKD " . $mkdir);
curl_setopt($ch, CURLOPT_URL, $realserver);
curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $pass);
curl_exec($ch);
curl_close($ch);
?>
7. 권한을 설정하는 방법
<?php
// 서버 경로
$realserver = 'ftp://habonyphp.com/public_html/data/';
// 서버 사용자 정보
$user = 'habony';
$pass = '123456';
// 설정할 폴더/파일
$fname = 'data';
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTQUOTE, array("SITE CHMOD 0707 " . $fname);
curl_setopt($ch, CURLOPT_URL, $realserver);
curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $pass);
curl_exec($ch);
curl_close($ch);
?>
8. 파일 목록을 출력하는 방법
<?php
// 서버 경로
$realserver = 'ftp://habonyphp.com/public_html/data/';
// 서버 사용자 정보
$user = 'habony';
$pass = '123456';
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $realserver);
curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $pass);
$list = curl_exec($ch);
curl_close($ch);
print_r($list);
?>
0 댓글