노력이 좋아서

<step52>'php_이미지 전송'

zoaseo 2022. 6. 2. 12:50

1) ex16_img_process.php

<?php
    $file = $_FILES['img'];
    var_dump($file);
    // 파일 업로드시 임시 저장 위치
    echo $file['tmp_name'];
    // 실제 저장하고 싶은 위치 C:Apache24/htdocs/php/
    // 업로드된 파일을 내가 지정한 위치에 지정한 파일명으로 파일을 이동
    // move_uploaded_file(현재 위치, 이동할 위치)
    $result = move_uploaded_file($file['tmp_name'], 'C:Apache24/htdocs/php/'.$file['name']);
    if($result){
?>
        <img src="<?=$file['name']?>">
<?php
    }
?>

2) ex16_imgupload.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<!-- 
    파일전송이 form태그 속성 추가하기
    "multipart/form-data" 
-->
    <form action="ex16_img_process.php" method="post"
    enctype="multipart/form-data">
    이미지업로드 : <input type="file" name="img"><button type="submit">확인</button>
    </form>
</body>
</html>