PHP 동영상(파일) 업로드 하기
페이지 정보
본문
동영상 파일을 업로드 하는 방법을 정리합니다.
1. 디비 테이블
CREATE TABLE IF NOT EXISTS `g5_movie` (
`mo_idx` int(11) NOT NULL auto_increment,
`mo_mbid` varchar(30) NOT NULL,
`mo_subject` varchar(200) NOT NULL,
`mo_theme` varchar(100) default NULL,
`mo_memo` text,
`mo_video` varchar(100) default NULL,
`mo_video_name` varchar(100) NOT NULL,
`mo_thumb` varchar(100) default NULL,
`mo_elidx` int(11) default NULL,
`mo_elder_text` varchar(100) NOT NULL,
`mo_regdate` datetime default NULL,
`mo_status` varchar(10) NOT NULL,
`mo_hit` int(11) NOT NULL default '0',
`publish_agree` varchar(100) default NULL,
PRIMARY KEY (`mo_idx`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
2. dbconfig.php
<?php
$ftp_server="서버아이피";
$ftp_user="FTP아이디";
$ftp_pass="FTP비밀번호";
$ftp_port="FTP포트";
$ftp_root="/www"; // FTP 접속 상위폴더가 있으면 여기에 적습니다.
?>
3. 동영상 저장폴더 생성
/data/movie
4. video.php
<html>
<head>
<title>동영상 올리기<title>
</head>
<?php
if($mo_idx){
$query="select * from g5_movie where mo_idx='$mo_idx'";
$rows=sql_fetch($query);
$mode="modify";
$is_upload="1";
}else{
$mode="write";
$is_upload="0";
}
?>
<body>
<div class="order_wrap" style="padding:0px;">
<form name="writeform" method=post action="video_update.php" enctype="multipart/form-data">
<input type=hidden name="mode" value="<?php echo $mode?>">
<input type=hidden name="mo_idx" value="<?php echo $mo_idx?>">
<input type=hidden name="is_upload" id="is_upload" value="<?php echo $is_upload?>">
<table class="or_tb03" style="border:0px;">
<tr>
<td>
<input type="file" name="mo_video" id="mo_video" style="width:50%">
<?php if($rows['mo_video']){?>
<a href="javascript:viewmovie(<?php echo $mo_idx;?>)" class="btn01">미리보기</a>
<a href="/data/movie<?php echo $rows['mo_video'];?>" class="btn01">다운로드</a>
<?php }?>
<span id="upload_btn"><a href="javascript:movie_save()" class="btn01"> 업로드 </a></span>
<span style="display:block;color:#ff6600;margin-top:10px; font-weight:bold; font-size:14px;">
동영상은 최대 20Mb, 영상확장자가 MP4일 때 업로드 가능합니다.
</span>
</td>
</tr>
</table>
</form>
</div>
<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");
}
/**
if(flag){
alert(((iSize/1024)/1024).toFixed(1)+"MB");
} else {
alert((iSize/1024).toFixed(1)+"KB");
}
*/
});
</script>
</body>
</html>
5. video_update.php
<?php
include "dbcongif.php";
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='video.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");
//echo "Dir: ".ftp_pwd($conn_id);
ftp_chdir($conn_id, $ftp_root."/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'];
echo $file_name ."<br>";
$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;
//echo "remote_file = ". $remote_file ."<br>";
if ($strExt=="txt") {
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
} else {
$msg="파일업로드에 실패하였습니다.";
}
} elseif ($strExt=="mp4" || $strExt=="jpg" || $strExt=="jpeg" || $strExt=="gif" || $strExt=="png" || $strExt=="tar" || $strExt=="gz" || $strExt=="zip" || $strExt=="7z") {
if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
} else {
$msg="파일업로드에 실패하였습니다.";
}
} else {
$msg = "허용된 확장자가 아닙니다";
echo "<script>alert('".$msg."');history(-1);</script>";
exit;
}
// close the connection
ftp_close($conn_id);
//exit;
if($msg){
echo "<script>alert('".$msg."');parent.location.href='video.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='video.php';</script>";
exit;
}
$conn_id = ftp_connect($ftp_server,$port);
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);
@ftp_chdir($conn_id, $ftp_root."/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 ($strExt=="txt") {
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
} else {
$msg="파일업로드에 실패하였습니다.";
}
} elseif ($strExt=="mp4" || $strExt=="jpg" || $strExt=="jpeg" || $strExt=="gif" || $strExt=="png" || $strExt=="tar" || $strExt=="gz" || $strExt=="zip" || $strExt=="7z") {
if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
} else {
$msg="파일업로드에 실패하였습니다.";
}
} else {
$msg = "허용된 확장자가 아닙니다";
echo "<script>alert('".$msg."');history(-1);</script>";
exit;
}
// close the connection
ftp_close($conn_id);
if($msg){
//echo "<script>alert('$msg');history(-1);</ script>";
echo "<script>alert('".$msg."');parent.location.href='video.php';</script>";
}else{
echo "업로드가 완료 되었습니다.";
echo "<script>parent.insert_video('/".$dir."/".$remote_file."','".$file_name."');</script>";
}
}
}
?>
6. video_preview.php
<?php
include_once('dbconfig.php');
$query="select * from g5_movie where mo_idx='".$mo_idx."'";
$rows=sql_fetch($query);
?>
<video poster="/data/video_thumb<?php echo $rows['mo_thumb']?>" controls style="max-width:640px;" width="640px">
<source src="/data/movie<?php echo $rows['mo_video']?>" type="video/mp4">
</video>
참고자료
https://firejune.com/1332/PHP에서+FTP함수의++종류와++사용법
1. 디비 테이블
CREATE TABLE IF NOT EXISTS `g5_movie` (
`mo_idx` int(11) NOT NULL auto_increment,
`mo_mbid` varchar(30) NOT NULL,
`mo_subject` varchar(200) NOT NULL,
`mo_theme` varchar(100) default NULL,
`mo_memo` text,
`mo_video` varchar(100) default NULL,
`mo_video_name` varchar(100) NOT NULL,
`mo_thumb` varchar(100) default NULL,
`mo_elidx` int(11) default NULL,
`mo_elder_text` varchar(100) NOT NULL,
`mo_regdate` datetime default NULL,
`mo_status` varchar(10) NOT NULL,
`mo_hit` int(11) NOT NULL default '0',
`publish_agree` varchar(100) default NULL,
PRIMARY KEY (`mo_idx`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
2. dbconfig.php
<?php
$ftp_server="서버아이피";
$ftp_user="FTP아이디";
$ftp_pass="FTP비밀번호";
$ftp_port="FTP포트";
$ftp_root="/www"; // FTP 접속 상위폴더가 있으면 여기에 적습니다.
?>
3. 동영상 저장폴더 생성
/data/movie
4. video.php
<html>
<head>
<title>동영상 올리기<title>
</head>
<?php
if($mo_idx){
$query="select * from g5_movie where mo_idx='$mo_idx'";
$rows=sql_fetch($query);
$mode="modify";
$is_upload="1";
}else{
$mode="write";
$is_upload="0";
}
?>
<body>
<div class="order_wrap" style="padding:0px;">
<form name="writeform" method=post action="video_update.php" enctype="multipart/form-data">
<input type=hidden name="mode" value="<?php echo $mode?>">
<input type=hidden name="mo_idx" value="<?php echo $mo_idx?>">
<input type=hidden name="is_upload" id="is_upload" value="<?php echo $is_upload?>">
<table class="or_tb03" style="border:0px;">
<tr>
<td>
<input type="file" name="mo_video" id="mo_video" style="width:50%">
<?php if($rows['mo_video']){?>
<a href="javascript:viewmovie(<?php echo $mo_idx;?>)" class="btn01">미리보기</a>
<a href="/data/movie<?php echo $rows['mo_video'];?>" class="btn01">다운로드</a>
<?php }?>
<span id="upload_btn"><a href="javascript:movie_save()" class="btn01"> 업로드 </a></span>
<span style="display:block;color:#ff6600;margin-top:10px; font-weight:bold; font-size:14px;">
동영상은 최대 20Mb, 영상확장자가 MP4일 때 업로드 가능합니다.
</span>
</td>
</tr>
</table>
</form>
</div>
<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");
}
/**
if(flag){
alert(((iSize/1024)/1024).toFixed(1)+"MB");
} else {
alert((iSize/1024).toFixed(1)+"KB");
}
*/
});
</script>
</body>
</html>
5. video_update.php
<?php
include "dbcongif.php";
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='video.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");
//echo "Dir: ".ftp_pwd($conn_id);
ftp_chdir($conn_id, $ftp_root."/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'];
echo $file_name ."<br>";
$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;
//echo "remote_file = ". $remote_file ."<br>";
if ($strExt=="txt") {
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
} else {
$msg="파일업로드에 실패하였습니다.";
}
} elseif ($strExt=="mp4" || $strExt=="jpg" || $strExt=="jpeg" || $strExt=="gif" || $strExt=="png" || $strExt=="tar" || $strExt=="gz" || $strExt=="zip" || $strExt=="7z") {
if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
} else {
$msg="파일업로드에 실패하였습니다.";
}
} else {
$msg = "허용된 확장자가 아닙니다";
echo "<script>alert('".$msg."');history(-1);</script>";
exit;
}
// close the connection
ftp_close($conn_id);
//exit;
if($msg){
echo "<script>alert('".$msg."');parent.location.href='video.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='video.php';</script>";
exit;
}
$conn_id = ftp_connect($ftp_server,$port);
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);
@ftp_chdir($conn_id, $ftp_root."/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 ($strExt=="txt") {
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
} else {
$msg="파일업로드에 실패하였습니다.";
}
} elseif ($strExt=="mp4" || $strExt=="jpg" || $strExt=="jpeg" || $strExt=="gif" || $strExt=="png" || $strExt=="tar" || $strExt=="gz" || $strExt=="zip" || $strExt=="7z") {
if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
} else {
$msg="파일업로드에 실패하였습니다.";
}
} else {
$msg = "허용된 확장자가 아닙니다";
echo "<script>alert('".$msg."');history(-1);</script>";
exit;
}
// close the connection
ftp_close($conn_id);
if($msg){
//echo "<script>alert('$msg');history(-1);</ script>";
echo "<script>alert('".$msg."');parent.location.href='video.php';</script>";
}else{
echo "업로드가 완료 되었습니다.";
echo "<script>parent.insert_video('/".$dir."/".$remote_file."','".$file_name."');</script>";
}
}
}
?>
6. video_preview.php
<?php
include_once('dbconfig.php');
$query="select * from g5_movie where mo_idx='".$mo_idx."'";
$rows=sql_fetch($query);
?>
<video poster="/data/video_thumb<?php echo $rows['mo_thumb']?>" controls style="max-width:640px;" width="640px">
<source src="/data/movie<?php echo $rows['mo_video']?>" type="video/mp4">
</video>
참고자료
https://firejune.com/1332/PHP에서+FTP함수의++종류와++사용법
댓글목록
등록된 댓글이 없습니다.