PHP ftp 접속 로그인 만들기
페이지 정보
본문
1. movie.php
<form name="writeform" method=post action="movie_update.php" enctype="multipart/form-data">
<input type=hidden name="mode" value="<?php echo $mode; ?>">
<input type="file" name="mo_video" id="mo_video" style="width:50%">
<a href="javascript:movie_save()" class="btn01"> 업로드 </a>
</form>
<script>
function viewmovie(mo_idx){
var win=window.open("video_preview.php?mo_idx="+mo_idx,"preview_movie","width=650, height=650, scrollbars=no");
win.focus();
}
function movie_save(){
f=document.writeform;
if(f.mo_video.value==""){
alert("영상파일을 선택하세요");
return;
}
var fname = document.getElementById("mo_video").value;
var fext = fname.substr(fname.length-3).toLowerCase();
if(fext!="mp4"){
alert("영상확장자가 mp4 일때만 업로드 가능합니다.");
return;
}
if(f.is_upload.value!="1"){
alert("파일용량이 20M를 넘었습니다.");
return;
}
$("#upload_btn").html("업로드중...");
f.submit();
}
$('#mo_video').change(function(){
var f =this.files[0];
var flag = false;
var mbSize = 16;
if(f!=undefined){
var iSize = (f.size||f.fileSize);
if(iSize<20480000) flag=true;
}
if(flag){
$("#is_upload").val("1");
}else{
$("#is_upload").val("0");
}
});
</script>
2. movie_update.php
<?php
$ftp_server="서버주소"; // FTP 서버주소를 입력합니다.
$ftp_user="아이디"; // FTP 아이디를 입력합니다.
$ftp_pass="비밀번호"; // FTP 비밀번호를 입력합니다.
$ftp_port="21"; // FTP 접속포트를 입력합니다.
if($mode=="write"){
$file = $_FILES['mo_video']['tmp_name']; //tobe uploaded
if($_FILES['mo_video']['size']>=20480000){
$msg="파일용량이 20M를 넘었습니다.";
echo "<script>alert('".$msg."');parent.location.href='movie.php';</script>";
exit;
}
// set up basic connection
$conn_id = ftp_connect($ftp_server,$ftp_port) or die("Couldn't connect to $ftp_server");
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass) or die("Cannot login");
@ftp_chdir($conn_id, "data/movie");
$dir=date("Ym");
@ftp_mkdir($conn_id, $dir);
@ftp_chmod($conn_id, 0755, $dir);
@ftp_chdir($conn_id, $dir);
$file_name = $_FILES['mo_video']['name'];
$tmp = strpos(strrev($file_name), '.');
$temp = strlen($file_name) - $tmp;
$strName = substr($file_name, 0, $temp-1);
$strExt = substr($file_name, strlen($strName) + 1, strlen($file_name));
$remote_file = $member['mb_id']."_".time().".".$strExt;
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
} else {
$msg="파일업로드에 실패하였습니다.";
}
// close the connection
ftp_close($conn_id);
if($msg){
echo "<script>alert('".$msg."');parent.location.href='movie.php';</script>";
}else{
echo "업로드가 완료 되었습니다.";
echo "<script>parent.insert_video('/".$dir."/".$remote_file."','".$file_name."');</script>";
}
}
if($mode=="modify"){
$query="select * from g5_movie where mo_idx='".$mo_idx."'";
$rows=sql_fetch($query);
$file = $_FILES['mo_video']['tmp_name'];
if ($_FILES['mo_video']['name']) {
if($_FILES['mo_video']['size']>=20480000){
$msg="파일용량이 20M를 넘었습니다.";
echo "<script>alert('".$msg."');parent.location.href='movie.php';</script>";
exit;
}
$conn_id = ftp_connect($ftp_server,$port);
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);
@ftp_chdir($conn_id, "data/movie");
@ftp_delete($conn_id, $rows[mo_video]);
$dir=date("Ym");
@ftp_mkdir($conn_id, $dir);
@ftp_chmod($conn_id, 0755, $dir);
@ftp_chdir($conn_id, $dir);
$file_name = $_FILES['mo_video']['name'];
$tmp = strpos(strrev($file_name), '.');
$temp = strlen($file_name) - $tmp;
$strName = substr($file_name, 0, $temp-1);
$strExt = substr($file_name, strlen($strName) + 1, strlen($file_name));
$remote_file = $rows[mo_mbid]."_".time().".".$strExt;
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
} else {
$msg="파일업로드에 실패하였습니다.";
}
//exit;
// close the connection
ftp_close($conn_id);
if($msg){
echo "<script>alert('".$msg."');parent.location.href='movie.php';</script>";
}else{
echo "업로드가 완료 되었습니다.";
echo "<script>parent.insert_video('/{$dir}/{$remote_file}','$file_name');</script>";
}
}
}
?>
참고자료
http://php.net/manual/en/function.ftp-connect.php
댓글목록
등록된 댓글이 없습니다.