관리 메뉴

웹개발자의 기지개

[PHP] 영상 재생시 실제 URL 로 직접 접근하여 재생하기 방지 본문

PHP

[PHP] 영상 재생시 실제 URL 로 직접 접근하여 재생하기 방지

웹개발자 워니 2025. 5. 22. 00:11

 

보통의 경우 영상재생시에

HTML5 의 Video 태그의 <source src="/file/movie.mp4" type="video/mp4" /> 를 이용하여 간편하게 영상재생을 한다.

 

그런데, 이와 같을 경우 http://도메인 /file/movie.mp4 경로로 직접 브라우저에서 실행하면 영상이 재생되는 문제가 발생한다.

 

이를 위하여 아래와 같은 방법으로 나름 고안해 봤다.

 

[단계 1]

해당 file 폴더에 '.htaccess' 파일 생성

 

# /file/.htaccess
Order deny,allow
Deny from all

#Allow from all

 

[단계 2]

기존의 <source src="/file/movie.mp4" type="video/mp4" /> 태그의 직접 경로를 stream.php 파일이라는 별도의 파일을 만들고

스트림 방식으로 영상을 재생토록한다.

 

$file 은 파일명

<source src="stream.php?file=<?=$file?>" type="video/mp4" />

 

[stream.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?
session_start();
 
/*
// 로그인 체크
if (!isset($_SESSION['id'])) {
    http_response_code(403);
    die("Unauthorized");
}
*/
 
 
// 파일명 검증
$file = $_GET['file'] ?? '';
if (!$file || strpos($file'..'!== false) {
    http_response_code(400);
    die("Invalid file");
}
 
 
// 파일 경로 설정
//$filepath = __DIR__ . "/file/" . basename($file);
//$filepath = __DIR__ . "/file/" . $file;
 
// 물리적 경로
$filepath = "/www/file/" . basename($file);
 
 
 
// 파일 존재 여부 확인
if (!file_exists($filepath)) {
    http_response_code(404);
    die("File not found");
}
 
/*
// 비디오 스트리밍
header('Content-Type: video/mp4');
header('Content-Length: ' . filesize($filepath));
readfile($filepath);
exit;
*/
 
// 파일 핸들 열기
$fp = fopen($filepath'rb');
if (!$fp) {
    http_response_code(500);
    die("Cannot open file");
}
 
// 브라우저 캐시 방지
/*
header("Expires: 0");
header("Cache-Control: must-revalidate");
header("Pragma: public");
*/
header("Expires: 0");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
 
// 파일 전송 헤더
header('Content-Type: video/mp4');
header('Content-Length: ' . filesize($filepath));
header('Content-Disposition: inline'); // 다운로드가 아닌, 브라우저에서 재생만 허용
header('X-Content-Type-Options: nosniff');
 
// zlib.output_compression 끄기 (압축모듈 꺼야 제대로 전송됨)
if (ini_get('zlib.output_compression')) {
    ini_set('zlib.output_compression''Off');
}
 
// 조금씩 파일 읽어서 출력
$chunkSize = 8192// 8KB
while (!feof($fp)) {
    echo fread($fp$chunkSize);
    flush(); // 서버에서 클라이언트로 즉시 전송
}
 
fclose($fp);
exit;
 
?>
 
cs

 

 

 

 

 

 

Comments