Skip to content

Commit

Permalink
added validation controllers and views for video upload
Browse files Browse the repository at this point in the history
  • Loading branch information
kuldp18 committed Mar 14, 2024
1 parent 993fa73 commit 071506e
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 4 deletions.
36 changes: 36 additions & 0 deletions controllers/upload_video.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);


// check video file and title are not empty
function is_upload_input_empty(string $title, array $video): bool
{
if (empty($title) || empty($video)) {
return true;
}
return false;
}

// check if uploaded file is not a video in .mp4
function is_video_file_invalid(array $video): bool
{
$video_name = $video['name'];
$video_size = $video['size'];
$video_error = $video['error'];

$video_ext = explode('.', $video_name);
$video_actual_ext = strtolower(end($video_ext));

$allowed = ['mp4'];

if (in_array($video_actual_ext, $allowed)) {
if ($video_error === 0 && $video_size > 0) {
return false;
} else {
return true;
}
} else {
return true;
}
}
47 changes: 47 additions & 0 deletions includes/upload_video.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// get fields
$video = $_FILES['video'];
$title = $_POST['title'];
$description = $_POST['description'];
$thumbnail = $_FILES['thumbnail'];

try {
require_once "./db_handler.inc.php";
// require_once "../models/register.inc.php";
require_once "../controllers/upload_video.inc.php";

// Error handlers
$errors = [];

// Check for empty inputs: video and title

if (is_upload_input_empty($title, $video)) {
// make sure to use local variables here
$errors['empty_input'] = 'Please upload a video and provide a title';
} // Check if uploaded file is not a video
else if (is_video_file_invalid($video)) {
$errors['invalid_video'] = 'Please upload a valid video in .mp4 format';
}

require_once "./config_session.inc.php";

if ($errors) {
$_SESSION["errors_upload_video"] = $errors;
header('Location: ../pages/upload_video.php');
die();
}

// Upload video
// close connection
$pdo = null;
$stmt = null;
die();
} catch (PDOException $e) {
die("Error while uploading video: " . $e->getMessage());
}
} else {
header("Location: ../pages/upload_video.php");
exit();
}
13 changes: 9 additions & 4 deletions pages/upload_video.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<!-- <?php
require_once "../includes/config_session.inc.php";
?> -->
<?php
require_once "../includes/config_session.inc.php";
require_once "../views/upload_video.inc.php";
?>
<!DOCTYPE html>
<html lang="en">

Expand Down Expand Up @@ -29,7 +30,7 @@
<main class="upload">
<h2 class="upload__title">New video upload</h2>
<section class="upload__section">
<form action="" class="upload__section__form" method="post" enctype="multipart/form-data">
<form action="../includes/upload_video.inc.php" class="upload__section__form" method="post" enctype="multipart/form-data">
<div class="upload__section__form__item upload__section__form__item--video">
<label>Choose video:</label>
<input type="file" name="video" class="upload__input">
Expand All @@ -53,9 +54,13 @@
</section>
</main>

<?php check_and_print_video_upload_errors() ?>

<script>
$('textarea').autoResize();
</script>

<script src="../js/close_modal.js"></script>
</body>

</html>
21 changes: 21 additions & 0 deletions views/upload_video.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

function check_and_print_video_upload_errors()
{

if (isset($_SESSION["errors_upload_video"])) {
$errors = $_SESSION["errors_upload_video"];
if (count($errors) > 0) {
echo "<section class='modal modal--error'>";
echo "<h1 class='modal__title'>Unable to upload your video: </h1>";
echo "<span class='modal__close modal__close--error'>X</span>";
foreach ($errors as $error) {
echo "<p class='modal__item'>$error</p>";
}
echo "</section>";
unset($_SESSION["errors_upload_video"]);
}
}
}

0 comments on commit 071506e

Please sign in to comment.