Game-Jaming/Backend/Controllers/FileHandler/upload.php

60 lines
2.3 KiB
PHP

<?php
require_once "../../../bootstrap.php";
use Backend\Models\GameData;
if(isset($_POST['submitUpload'])){
//Get the data from the user form
$gameFile = $_FILES['gameFile'];
$desc = $_POST['description'];
$title = $_POST['gameTitle'];
$thumbnail = $_FILES['thumbnailFile'];
$isWebBased = isset( $_POST['isWebBased']);
//Handle data for game files
$gameFileName = $_FILES['gameFile']['name'];//Game name
$gameFileTmp = $_FILES['gameFile']['tmp_name'];//Tmp location of the file
$gameFileError =$_FILES['gameFile']['error'];//File error
$fileExtGame = explode('.', $gameFileName);
$fileActualExtGame = strtolower(end($fileExtGame));
//Handle data for thumbnail files
$thumbnailFileName = $_FILES['thumbnailFile']['name'];//Game name
$thumbnailFileTmp = $_FILES['thumbnailFile']['tmp_name'];//Tmp location of the file
$thumbnailFileError =$_FILES['thumbnailFile']['error'];//File error
$fileExtThumb = explode('.', $thumbnailFileName);
$fileActualExtThumb = strtolower(end($fileExtThumb));
$allowedFileTypeGame = array('zip');
$allowedFileTypeThumbnail = array('gif', 'jpeg', 'png', 'svg');
if(in_array($fileActualExtGame,$allowedFileTypeGame) || in_array($thumbnailFileName,$allowedFileTypeThumbnail)){
if($gameFileError === 0 || $thumbnailFileError === 0){
$gameFileNewName = uniqid("", true). "." . $fileActualExtGame;
$thumbnailFileNewName = uniqid("", true). "." . $fileActualExtThumb;
if(empty($gameFileName)){
header("location: ../../../Frontend/index.php?error=emptyFile");
exit();
}else{
$gameData = new GameData();
$gameData->game_name = $title;
$gameData->game_link = $gameFileNewName;
$gameData->description = $desc;
$gameData->img = $thumbnailFileNewName;
$gameData->is_web_Based = $isWebBased;
$gameData->save();
rename($gameFileTmp,"../../Games/".$gameFileNewName);
rename($thumbnailFileTmp,"../../../Frontend/images/".$fileActualExtThumb);
}
}else{
echo "Der var en fejl med at uploade din file";
}
}else{
echo "Wrong file type";
}
}