📖
PHP 파일명 숨기기 토큰 매핑 or 해시 매핑
페이지 정보
본문
✅ 방법 1: 토큰 매핑 (추천)
파일명은 서버에서만 알고, 사용자에겐 랜덤 토큰만 넘김[code]// 링크 생성
$token = sha1($fileNameWithSubdir . microtime(true)); // 랜덤한 토큰
$_SESSION['filemap'][$token] = $fileNameWithSubdir; // 세션이나 DB에 매핑 저장
echo "<a href='download.php?id={$token}'>다운로드</a>";[/code][code]// 다운로드
session_start();
$fileid = $_GET['id'] ?? '';
$fi_decode = $_SESSION['filemap'][$fileid] ?? null;
if (!$fi_decode) {
echo "<script>alert('잘못된 접근입니다.');history.back();window.close();</script>";
exit;
}
$file = $fi_decode;[/code]👉 이렇게 하면 URL에는 id=랜덤값 만 보이고, 실제 파일명은 절대 노출되지 않습니다.
✅ 방법 2: 해시 매핑 (DB 기반)
DB 테이블에 hash → 파일명 저장해두고 꺼내 쓰기.[code]// 링크 생성
$hash = md5($fileNameWithSubdir);
$db->query("INSERT INTO filemap (hash, filepath) VALUES ('$hash', '$fileNameWithSubdir')");
echo "<a href='download.php?id={$hash}'>다운로드</a>";
[/code][code]// 다운로드
$hash = $_GET['id'] ?? '';
$result = $db->query("SELECT filepath FROM filemap WHERE hash='$hash'");
$row = $result->fetch_assoc();
$fi_decode = $row['filepath'] ?? null;
$file = $fi_decode;[/code]👉 URL에는 해시만 남고, 파일명은 서버에서만 확인 가능.
✅ 방법 3: 무작위 토큰 (랜덤 ID) 매핑[code]// 링크 생성
$token = bin2hex(random_bytes(8)); // 16자리 토큰
$_SESSION['filemap'][$token] = $fileNameWithSubdir;
echo "<a href='download.php?f={$token}'>다운로드</a>";[/code][code]// 다운로드
session_start();
$token = $_GET['f'] ?? '';
if (!isset($_SESSION['filemap'][$token])) {
exit("잘못된 접근입니다.");
}
$realFile = $_SESSION['filemap'][$token];
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename*=UTF-8''".rawurlencode(basename($realFile)));
readfile($realFile);[/code]👉 장점
URL에 한글 안 들어감
보안성 ↑ (사용자는 절대 실제 파일명 모름)
✅ 방법 4: 해시 (MD5 / SHA1) 이용
파일명을 해싱해서 노출 → 서버에서 다시 매핑[code]// 링크 생성
$hash = md5($fileNameWithSubdir);
saveMapping($hash, $fileNameWithSubdir); // DB나 캐시에 저장
echo "<a href='download.php?h={$hash}'>다운로드</a>";[/code][code]// 다운로드
$hash = $_GET['h'] ?? '';
$realFile = getFileFromHash($hash); // DB 조회
if (!$realFile) exit("파일 없음");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename*=UTF-8''".rawurlencode(basename($realFile)));
readfile($realFile);[/code]👉 장점
추측 불가능한 값
서버에서만 실제 파일명 확인 가능
파일명은 서버에서만 알고, 사용자에겐 랜덤 토큰만 넘김[code]// 링크 생성
$token = sha1($fileNameWithSubdir . microtime(true)); // 랜덤한 토큰
$_SESSION['filemap'][$token] = $fileNameWithSubdir; // 세션이나 DB에 매핑 저장
echo "<a href='download.php?id={$token}'>다운로드</a>";[/code][code]// 다운로드
session_start();
$fileid = $_GET['id'] ?? '';
$fi_decode = $_SESSION['filemap'][$fileid] ?? null;
if (!$fi_decode) {
echo "<script>alert('잘못된 접근입니다.');history.back();window.close();</script>";
exit;
}
$file = $fi_decode;[/code]👉 이렇게 하면 URL에는 id=랜덤값 만 보이고, 실제 파일명은 절대 노출되지 않습니다.
✅ 방법 2: 해시 매핑 (DB 기반)
DB 테이블에 hash → 파일명 저장해두고 꺼내 쓰기.[code]// 링크 생성
$hash = md5($fileNameWithSubdir);
$db->query("INSERT INTO filemap (hash, filepath) VALUES ('$hash', '$fileNameWithSubdir')");
echo "<a href='download.php?id={$hash}'>다운로드</a>";
[/code][code]// 다운로드
$hash = $_GET['id'] ?? '';
$result = $db->query("SELECT filepath FROM filemap WHERE hash='$hash'");
$row = $result->fetch_assoc();
$fi_decode = $row['filepath'] ?? null;
$file = $fi_decode;[/code]👉 URL에는 해시만 남고, 파일명은 서버에서만 확인 가능.
✅ 방법 3: 무작위 토큰 (랜덤 ID) 매핑[code]// 링크 생성
$token = bin2hex(random_bytes(8)); // 16자리 토큰
$_SESSION['filemap'][$token] = $fileNameWithSubdir;
echo "<a href='download.php?f={$token}'>다운로드</a>";[/code][code]// 다운로드
session_start();
$token = $_GET['f'] ?? '';
if (!isset($_SESSION['filemap'][$token])) {
exit("잘못된 접근입니다.");
}
$realFile = $_SESSION['filemap'][$token];
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename*=UTF-8''".rawurlencode(basename($realFile)));
readfile($realFile);[/code]👉 장점
URL에 한글 안 들어감
보안성 ↑ (사용자는 절대 실제 파일명 모름)
✅ 방법 4: 해시 (MD5 / SHA1) 이용
파일명을 해싱해서 노출 → 서버에서 다시 매핑[code]// 링크 생성
$hash = md5($fileNameWithSubdir);
saveMapping($hash, $fileNameWithSubdir); // DB나 캐시에 저장
echo "<a href='download.php?h={$hash}'>다운로드</a>";[/code][code]// 다운로드
$hash = $_GET['h'] ?? '';
$realFile = getFileFromHash($hash); // DB 조회
if (!$realFile) exit("파일 없음");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename*=UTF-8''".rawurlencode(basename($realFile)));
readfile($realFile);[/code]👉 장점
추측 불가능한 값
서버에서만 실제 파일명 확인 가능
댓글목록
등록된 댓글이 없습니다.
![]() ![]() |