-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.php
163 lines (144 loc) · 4.79 KB
/
upload.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
session_start();
if (!isset($_SESSION["username"])) {
header("Location: login.php");
exit();
}
$username = $_SESSION["username"];
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$title = $_POST["title"];
$videoFilename = $_FILES["video"]["name"];
$thumbnailFilename = $_FILES["thumbnail"]["name"];
// Upload video file
$videoUploadPath = "uploads/" . $videoFilename;
move_uploaded_file($_FILES["video"]["tmp_name"], $videoUploadPath);
// Upload thumbnail file
$thumbnailUploadPath = "thumbnails/" . $thumbnailFilename;
move_uploaded_file($_FILES["thumbnail"]["tmp_name"], $thumbnailUploadPath);
// Update videos.json with new video data
$videosData = json_decode(file_get_contents("videos.json"), true);
$newVideoData = array(
"video_title" => $title,
"filename" => $videoFilename,
"thumbnail" => $thumbnailUploadPath,
"username" => $username,
"views" => 0,
"likes" => 0,
"dislikes" => 0
);
$videosData[] = $newVideoData;
file_put_contents("videos.json", json_encode($videosData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
// Return a JSON response indicating success
header("Content-Type: application/json");
echo json_encode(["success" => true]);
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Upload Video</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
margin: 0;
padding: 0;
}
h1 {
text-align: center;
margin-top: 20px;
}
#upload-form {
max-width: 400px;
margin: 20px auto;
padding: 20px;
background-color: #fff;
border: 1px solid #ccc;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input[type="text"],
input[type="file"] {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 3px;
}
input[type="submit"] {
padding: 10px 20px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 3px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #0056b3;
}
a {
display: block;
text-align: center;
margin-top: 10px;
color: #007bff;
text-decoration: none;
}
</style>
</head>
<body>
<h1>Upload Video</h1>
<form id="upload-form" enctype="multipart/form-data" method="POST">
<label for="title">Video Title:</label>
<input type="text" id="title" name="title" required>
<br>
<label for="video">Select Video:</label>
<input type="file" id="video" name="video" accept="video/*" required>
<br>
<label for="thumbnail">Select Thumbnail:</label>
<input type="file" id="thumbnail" name="thumbnail" accept="image/*" required>
<br>
<input type="submit" value="Upload">
</form>
<br>
<a href="dashboard.php">Back to Dashboard</a>
<script>
document.addEventListener("DOMContentLoaded", function () {
const uploadForm = document.getElementById("upload-form");
uploadForm.addEventListener("submit", function (event) {
event.preventDefault(); // Prevent the default form submission
uploadVideo();
});
});
function uploadVideo() {
const form = document.getElementById("upload-form");
const formData = new FormData(form);
fetch("upload.php", {
method: "POST",
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert("Video uploaded successfully");
// Redirect to dashboard after successful upload
window.location.href = "dashboard.php";
} else {
alert("Error uploading video");
}
})
.catch(error => {
console.error("Error:", error);
alert("An error occurred while uploading the video");
});
}
</script>
</body>
</html>