68 lines
1.7 KiB
PHP
68 lines
1.7 KiB
PHP
<?php
|
|
|
|
require("../../inc/connect.php");
|
|
$relative_dir = "../../";
|
|
|
|
$name = $_POST['name'];
|
|
$project = $_POST['project'];
|
|
$filetype = $_POST['type'];
|
|
$filename = basename($_FILES["file"]["name"]);
|
|
|
|
//Get the directory name
|
|
$query = "SELECT `uid` FROM project WHERE `id`=$project";
|
|
$temp = ExecQuery($query);
|
|
$result = $temp->fetch_assoc();
|
|
$dir = $relative_dir . "media/" . $result['uid'];
|
|
$target_file = $dir . "/" . $filename;
|
|
|
|
//Create the directory
|
|
if (! file_exists($dir)) {
|
|
mkdir($dir, 0755, true);
|
|
}
|
|
|
|
$extension = pathinfo($target_file,PATHINFO_EXTENSION);
|
|
|
|
// Allow certain file formats
|
|
$allowUpload = true;
|
|
if($filetype == "image")
|
|
{
|
|
if (! in_array($extension, array("jpg", "jpeg", "png", "gif"))) {
|
|
$allowUpload = false;
|
|
}
|
|
}
|
|
if($filetype == "video")
|
|
{
|
|
//if (! in_array(array("mp4"), $extension)) {
|
|
if($extension != "mp4"){
|
|
$allowUpload = false;
|
|
}
|
|
}
|
|
if($filetype == "audio")
|
|
{
|
|
//if (! in_array(array("mp3"), $extension)) {
|
|
if($extension != "mp3"){
|
|
$allowUpload = false;
|
|
}
|
|
}
|
|
|
|
//Check if upload is allowed
|
|
if(! $allowUpload)
|
|
{
|
|
header("Location: /ptf-admin/add?success=false");
|
|
exit;
|
|
}
|
|
|
|
//Try to move the file
|
|
if (! move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
|
|
header("Location: /ptf-admin/add?success=false");
|
|
exit;
|
|
}
|
|
|
|
//Update the database
|
|
$stmt = $mysqli->prepare("INSERT INTO media (`name`, `type`, `file`, `project-id`) VALUES (?,?,?,?)");
|
|
$stmt->bind_param("sssi", $name, $filetype, $filename, $project);
|
|
$stmt->execute();
|
|
$stmt->close();
|
|
|
|
header("Location: /ptf-admin/add?success=true");
|
|
?>
|