[PHP] HTML FORM 태그에서 사용하는 input 태그 - file

2022. 10. 11. 22:50Program/PHP

141_PHP HTML FORM 태그에서 사용하는 input 태그 - file

[요약]

  • input 태그를 사용하며 type 속성의 값으로 file을 사용하여 서버에 파일을 전송하는 폼에 대해 알아보자.

웹 서비스를 이용하다보면 자신이 찍은 사진이나 문서 파일을 업로드한 경험이 있을 것이다.

파일을 업로드하는 폼을 만들려면 input 태그의 type 속성의 값으로 file을 사용한다.


[type 속성값에 file을 적용하는 방법]

<input type="file" name="attachedFile" />

파일 업로드 폼을 만들때는 form 태그에 enctype 속성을 적용하고 값으로 multipart/form-data 를 사용한다.

작성하지 않으면 업로드한 파일의 이름(경로 포함)만 업로드되고 실제 파일은 업로드가 되지 않는다.

 

[form 태그 enctype 속성에 multipart/form-data 값 사용]

<form enctype='multipart/form-data'>
  <input type="file" name="attachedFile" />
</form>

 

다음은 파일업로드 폼 예제이다.

[예제: 144_input_file.php ]

<!DOCTYPE html>
<html lang="ko">
<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>INPUT 태그 type="file"</title>
</head>
<body>
  <form name="" method="" action="" enctype="multipart/form-data">
    파일 : <input type="file" name="attachedFile" />
  </form>
</body>
</html>

실행결과

위 그림에서 파일 선택을 눌러 업로드할 파일을 선택할 수 있다.

 

 

 


 

 

 

초보자를 위한 PHP 200제
김태영 지음 | 정보문화사