-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added validation controllers and views for video upload
- Loading branch information
Showing
4 changed files
with
113 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]); | ||
} | ||
} | ||
} |