My code looks like "spaghetti code". I have yet to use OOP in PHP but I am really interested in how a good programmer would handle this many if
statements. It looks like a mess.
<?php
session_start();
include './functions.php';
if(empty($_FILES['browseMovie']) || empty($_FILES['browseThumbnail']) || empty($_GET['title']) || empty($_GET['description']))
{
die('Please insert all fields.');
}
$filePartsMovie = pathinfo($_FILES['browseMovie']['name']);
$filePartsThumbnail = pathinfo($_FILES['browseThumbnail']['name']);
if($filePartsMovie['extension'] !== "mp4")
{
die('Video has to be a .mp4 file.');
}
if($filePartsThumbnail['extension'] !== "jpg" && $filePartsThumbnail['extension'] !== "png")
{
die('Thumbnail has to be in jpg/jpeg or png format.');
}
if($_FILES['browseMovie']['size'] + $_FILES['browseThumbnail']['size'] > 1600600000)
{
die('filesize of video and thumbnail = higher 1.5 GB.');
}
$id = $_SESSION["id"];
$name = $_SESSION["username"];
$folderName = generateUniqueFolder();
$title = mysql_real_escape_string($_GET['title']);
$description = mysql_real_escape_string($_GET['description']);
// the original file name will be stored in the database as well
$thumbFileName = basename($_FILES['browseThumbnail']['name']);
$vidFileName = basename($_FILES['browseMovie']['name']);
$userDir = "../users/$name/";
$videoDir = $userDir . "videos/" . $folderName . "/";
$uploadMovieDir = $videoDir . $vidFileName;
$uploadThumbnailDir = $videoDir . $thumbFileName;
if(!isLoggedIn() || !is_dir($userDir))
{
die('User does not have own folder. Please contact the administrator.');
}
if(!mkdir($videoDir))
{
die('Could not create unique video folder.');
}
if(!move_uploaded_file($_FILES['browseMovie']['tmp_name'], $uploadMovieDir))
{
rmdir($videoDir);
die('Could not upload the video correctly.');
}
if(!move_uploaded_file($_FILES['browseThumbnail']['tmp_name'], $uploadThumbnailDir))
{
removeTreeDir($videoDir);
die('Could not upload the thumbnail correctly.');
}
$con = connectDb();
$videoQuery = "INSERT INTO video(userId, title, description, foldername, vidFileName, thumbFileName) VALUES ($id,'$title','$description','$folderName', '$vidFileName', '$thumbFileName')";
if(!mysql_query($videoQuery))
{
removeTreeDir($videoDir);
die('Could not add the video to the database.');
}
mysql_close($con);
echo "true";
?>
Somewhere in the middle of my code I also need a bunch of variables. How would one clean that up? Create an object and aggregate them together?