Notice
Recent Posts
Recent Comments
Tags
- 404에러페이지
- django 엑셀불러오기
- 하드 마이그레이션
- php 캐쉬제거
- 강제이동
- swagger 500 error
- XSS방어
- asp.net core Select
- TempData
- SSD 복사
- ASP.Net Core 404
- 맥 오라클설치
- asp.net core swagger
- ViewBag
- 타임피커
- 말줄임표시
- javascript redirection
- asp ftp
- javascript 바코드 생성
- JavaScript
- jquery 바코드생성
- Mac Oracle
- 원격ftp
- asp.net Select
- asp.net dropdownlist
- XSS PHP
- simpe ftp
- ViewData
- 하드 윈도우 복사
- 바코드 생성하기
웹개발자의 기지개
[PHP] 영상 재생시 실제 URL 로 직접 접근하여 재생하기 방지 본문
보통의 경우 영상재생시에
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 |
'PHP' 카테고리의 다른 글
| [PHP] 유용한 실무팁 11 - 문자열 요소 병합 (중복은 하나만), 새로 추가된 문자열로 업데이트 (기존 요소가 없으면 바로 삭제) (0) | 2025.06.24 |
|---|---|
| [PHP] 현재 디렉토리 사용량 (0) | 2025.06.23 |
| [PHP] 배열내 특정 요소 값을 교체하기 (치환, 변경) (0) | 2025.03.27 |
| [PHP] 날짜 기간 사이 데이터값 불러오기 (timestamp값 이용) (2) | 2025.02.07 |
| [PHP] Ubuntu, Composer 설치하기 (0) | 2025.01.07 |
Comments