채택완료

첨부파일 데이터 저장

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

 

현재 코드는 이렇습니다.

Copy
<?php

include_once('./_common.php');

 

@mkdir(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(@mkdir($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개

채택된 답변
+20 포인트

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

Copy
$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">@mkdir</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

        // 다른 정보도 저장하려면 그만큼 코드를 추가시키면 되겠네요.

        // 그 외 여러 방법이 있는데 생략

    }

제작의뢰로 가셔야할거같아요

답변을 작성하려면 로그인이 필요합니다.