첨부파일 데이터 저장

첨부파일 데이터 저장

QA

첨부파일 데이터 저장

본문

이용자가 첨부파일을 등록하면 데이터서버에 저장을 하고싶습니다.

 

현재 코드는 이렇습니다.


<?php
include_once('./_common.php');
 
(G5_DATA_PATH.'/file/repair_request', G5_DIR_PERMISSION);
@chmod(G5_DATA_PATH.'/file/repair_request', G5_DIR_PERMISSION);
if($_FILES['u_file']) {
    $attach = $_FILES['u_file'];
    $name = preg_replace("/\.(php|phtm|htm|cgi|pl|exe|jsp|asp|inc)/i", "$0-x", $attach['name']);
    $name = substr(md5(uniqid($_SERVER['REQUEST_TIME'])),0,8).'_'.str_replace('%', '', urlencode($name));
    $ATTACH_DIR = G5_DATA_PATH.'/file/repair_request';
    $dest_file  = $ATTACH_DIR.$name;
    $url = $ATTACH_DIR.$name;
    if(!is_dir($ATTACH_DIR)) {
        if(($ATTACH_DIR, 0777, true)) {
            if(is_dir($ATTACH_DIR)) {
                @chmod($ATTACH_DIR, 0777);
            }
        }
    }
    if(move_uploaded_file($attach['tmp_name'], $dest_file)) {
        $attach_url = $url;
        $attach_name = $attach['name'];
        $attach_size = $attach['size'];
        $attach_type = $attach['type'];
    }
}
 
$femail = $_POST['femail'];
$temail = $_POST['temail'];
$remail = $_POST['remail'];
$hemail = $_POST['hemail'];
$subject = $_POST['tit'];
$message = $_POST['m_txt'];
$u_name = $_POST['u_name'];
$s_date = $_POST['s_date'];
$add = $_POST['add'];
$u_tel = $_POST['u_tel'];
$b_date = $_POST['b_date'];
 
$boundary = "----" . uniqid("part"); // 구분자
 
// --- 헤더 --- //
$headers = "Return-Path: ".str_replace("\\", "", $femail)."\r\n";
$headers .= "From: ".str_replace("\\", "", $femail)."\r\n";
if($remail) {
    $headers .= "Cc: ".str_replace("\\", "", $remail)."\r\n";
}
if($hemail) {
    $headers .= "Bcc: ".str_replace("\\", "", $hemail)."\r\n";
}
 
if($attach_url && $attach_name) { // --- 첨부파일 --- //
    $filename=$attach_name;
    $fp = fopen($attach_url,"r");
    $file = fread($fp,$attach_size);
    fclose($fp);
    if ($attach_type == ""){
        $attach_type = "application/octet-stream";
    }
    // --- 헤더 --- //
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: Multipart/mixed; boundary=\"$boundary\"";
    // --- 본문 --- // 
    $mailbody = "This is a multi-part message in MIME format.\r\n\r\n";
    $mailbody .= "--$boundary\r\n";
    $mailbody .= "Content-Type: text/html; charset=utf-8\r\n";
    $mailbody .= "Content-Transfer-Encoding: base64\r\n\r\n";
    $mailbody .= chunk_split(base64_encode(str_replace("\\", "", $s_date. "\r\n" .$add. "\r\n" .$u_name. "\r\n" .$u_tel. "\r\n"  .$b_date. "\r\n" .$message))) . "\r\n";
    // --- 첨부 --- // 
    $mailbody .= "--$boundary\r\n"; 
    $mailbody .= "Content-Type: ".$attach_type."; name=\"".$filename."\"\r\n";
    $mailbody .= "Content-Transfer-Encoding: base64\r\n";
    $mailbody .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $mailbody .= base64_encode($file)."\r\n\r\n";
    $mailbody .= "--$boundary--";
}
else {
    // --- 헤더 --- // 
    $headers .= "MIME-Version: 1.0\r\n"; 
    $headers .= "Content-Type: Multipart/alternative; boundary = \"$boundary\"";
    // --- 본문 --- //
    $mailbody = "--$boundary\r\n"; 
    $mailbody .= "Content-Type: text/html; charset=utf-8\r\n";
    $mailbody .= "Content-Transfer-Encoding: base64\r\n\r\n";
    $mailbody .= chunk_split(base64_encode(str_replace("\\", "", $s_date. "\r\n" .$add. "\r\n" .$u_name. "\r\n" .$u_tel. "\r\n"  .$b_date. "\r\n" .$message))) . "\r\n";
    $mailbody .= "--$boundary--\r\n\r\n"; 
}
 
$ret = mail($temail, $subject, $mailbody, $headers);
 
if(!$tit || !$u_name|| !$u_tel){
    alert('잘못된 접근입니다.');
    exit;
}
 
$file = "";

 
$sql = "
insert into 
    repair_request
set
    `tit`       = '".$tit."'    
,   `s_date`    = '".$s_date."' 
,   `add`       = '".$add."'    
,   `u_name`    = '".$u_name."' 
,   `u_tel`     = '".$u_tel."'  
,   `b_date`    = '".$b_date."' 
,   `m_txt`     = '".$m_txt."'  
,   `u_pw`      = '".$u_pw."'   
,   `file`      = '".$file."'   
";
 
sql_query($sql);
 
$request_idx = sql_insert_id();
$ss_name = 'repair_request_'.$request_idx;
set_session($ss_name, TRUE);
 
goto_url("./sub0603.php?idx=".$request_idx);
?>
 

이 질문에 댓글 쓰기 :

답변 2

데이터 서버는 다른 곳인가요?


    $ATTACH_DIR = G5_DATA_PATH.'/file/repair_request';
    $dest_file  = $ATTACH_DIR.$name;
    $url = $ATTACH_DIR.$name;
    if(!is_dir($ATTACH_DIR)) {
        if(<a href="//sir.kr/main/member/?mb_id=sterian" class="mention_user_info" target="_blank"></a>($ATTACH_DIR, 0777, true)) {
            if(is_dir($ATTACH_DIR)) {
                @chmod($ATTACH_DIR, 0777);
            }
        }
    }
    if(move_uploaded_file($attach['tmp_name'], $dest_file)) {
        $attach_url = $url;
        $attach_name = $attach['name'];
        $attach_size = $attach['size'];
        $attach_type = $attach['type'];
        // 서버에 저장된 파일을 다른 곳에 올리고 싶다면 여기에서 ftp 함수 사용, 아래 참고
        // https://www.php.net/manual/en/ftp.examples-basic.php
        // 다른 정보도 저장하려면 그만큼 코드를 추가시키면 되겠네요.
        // 그 외 여러 방법이 있는데 생략
    }
답변을 작성하시기 전에 로그인 해주세요.
전체 38
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT