본문 바로가기
카테고리 없음

한글 이미지 파일 이름 깨짐 방지하는 방법

by 오디와이 2025. 4. 23.

한글 이미지 파일 이름 깨짐 방지하는 방법

PHP, ASP, JSP에서 한글 이미지 파일 이름 깨짐 방지하는 방법

웹사이트에서 한글로 된 이미지 파일을 다운로드하거나 표시할 때, 파일명이 깨지는 현상을 겪은 적 있지 않으신가요?
이번 글에서는 PHP, ASP, JSP에서 내장 함수를 이용해 깨짐 없이 한글 이미지 파일을 안전하게 출력하는 방법을 소개합니다.


✅ PHP에서 한글 파일명 깨짐 방지

iconv()basename()을 조합하면, 브라우저에 맞춰 인코딩 처리를 할 수 있습니다.

<?php
$file = "이미지파일.jpg";

// EUC-KR 인코딩으로 변환 (Windows IE 등 호환)
$encoded_filename = iconv("UTF-8", "EUC-KR", basename($file));

header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$encoded_filename\"");
readfile($file);
?>

✅ ASP에서 한글 파일명 깨짐 방지

ASP는 Server.URLEncode()라는 내장 함수를 활용하면 간단하게 해결됩니다.

<%
Dim fileName
fileName = "한글사진.jpg"

Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Disposition", "attachment; filename=" & Server.URLEncode(fileName)
Response.WriteFile(Server.MapPath(fileName))
Response.End
%>

✅ JSP에서 한글 파일명 깨짐 방지

JSP에서는 자바의 내장 클래스 URLEncoder를 사용해 UTF-8로 인코딩할 수 있습니다.

<%
String fileName = "한글파일.jpg";

// UTF-8 인코딩
String encodedFileName = java.net.URLEncoder.encode(fileName, "UTF-8");

response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedFileName + "\";");
%>

📌 각 언어별 요약

언어 사용 함수 주요 기능
PHP iconv(), basename() 파일명 인코딩 변환 및 안전한 추출
ASP Server.URLEncode() 한글 파일명 URL 인코딩
JSP URLEncoder.encode() UTF-8 인코딩 처리

💡 마무리 팁

  • 파일 이름은 가급적 영문자+숫자+언더바(_) 조합을 사용하는 것이 좋습니다.
  • 그래도 한글 파일명을 써야 할 경우, 위 예제처럼 내장 함수로 인코딩해 주는 것이 핵심입니다.
  • 브라우저 및 운영체제에 따라 다르게 처리될 수 있으므로 테스트는 꼭 필요합니다.

✔ 요약

  • PHP: iconv()로 인코딩 변환
  • ASP: Server.URLEncode()로 간단 처리
  • JSP: URLEncoder.encode() 사용

한글 파일명 깨짐 문제는 누구나 겪을 수 있습니다.

 

위 방법을 적용하면 사용자에게 보다 나은 경험을 제공할 수 있어요 😊