[자바스크립트]

var index = 0부터

파일첨부 버튼이 동적으로 생성될때마다 index++

 

파일 첨부 버튼을 동적으로 생성하고자 한다면 아래의 링크로....

https://mygameprogamming.tistory.com/66?category=711768

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 파일 전송
    $('.jeompo_register_btn').on('click',function(e){
        var formData = new FormData();
        
        var i;
        for( i = 0 ; i < index ; i++ ){
            var name = "file_" + i;
            formData.append( name , $("#image_plus_" + i)[0].files[0]);
        }
        
        formData.append("idx", index);
 
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'http://ip주소/pass_register.php',
            data: formData,
            contentType: false,
            processData: false,
            success : function(result){
                alert(result);
            },
            error : function (xtr,status,error){
                alert(xtr + ":" + status + ":" + error);
            }
        });
    });
 

[php]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
    include "../connectDB.php";
    
    $index = $_POST['idx'];
    $fileArr = array();
    $image_dir = $_SERVER['DOCUMENT_ROOT'];     //웹서버 설치 경로
    $check = true;
    for($i = 0 ; $i < $index ;$i++){
        $fileArr[$i= $_FILES["file_$i"]['name'];                                                               //파일 이름
        $tmp_name = $_FILES["file_$i"]['tmp_name'];                                                              //파일 임시저장 경로
        $fileArr[$i= preg_replace("/[ #\&\+\%@=\/\\\:;,\'\"\^`~\|\!\?\*$#<>()\[\]\{\}]/i"""$fileArr[$i]); //특수문자제거
        $fileArr[$i= iconv("utf-8""euc-kr"$fileArr[$i]);                                                   //한글 인코딩
        $save_dir = $image_dir.'/image_box/'.$fileArr[$i];                                                       //실제 저장 경로
        if(!move_uploaded_file($tmp_name,$save_dir)){
            $check = false;
            break;
        }
    }
    if($check){
        echo "success";
    }else{
        echo "fail";
    }
 
    mysqli_close($db);
?>
 
 

+ Recent posts