dj_mix_hosting_software/classes/Upload.php
Cody Cook 74f1d6e193
Some checks failed
SonarQube Scan / SonarQube Trigger (push) Failing after 53s
PHPUnit stuff.
2024-05-14 17:54:06 -07:00

91 lines
2.4 KiB
PHP

<?php
namespace DJMixHosting;
class Upload
{
private $file;
private $file_name;
private $file_size;
private $file_tmp;
private $file_type;
private $file_ext;
private $file_path;
private $extensions = array("mp3", "zip");
private $upload_dir = "uploads/";
private $errors = array();
private $ok = false;
private $config;
private $uuid = "";
public function __construct($file, $config)
{
$this->file = $file;
$this->file_name = $file['name'];
$this->file_size = $file['size'];
$this->file_tmp = $file['tmp_name'];
$this->file_type = $file['type'];
$this->config = $config;
$ext = explode('.', $file['name']);
$this->file_ext = strtolower(end($ext));
$this->uuid = uniqid();
}
private function check_file_size(): bool
{
if ($this->file_size > $this->config['uploads']['max_file_size']){
$this->errors[] = "File size is too large";
return false;
}
return true;
}
private function check_file_extension(): bool
{
if (!in_array($this->file_ext, $this->extensions)){
$this->errors[] = "Invalid file extension";
return false;
}
return true;
}
private function check_file_exists(): bool
{
if (file_exists($this->upload_dir . $this->file_name)){
$this->errors[] = "File already exists";
return false;
}
return true;
}
private function move_file(): bool
{
if (move_uploaded_file($this->file_tmp, $this->upload_dir . $this->uuid)){
$this->file_path = $this->upload_dir . $this->uuid;
return true;
}
return false;
}
public function dump_all()
{
$array = array(
"file" => $this->file,
"file_name" => $this->file_name,
"file_size" => $this->file_size,
"file_tmp" => $this->file_tmp,
"file_type" => $this->file_type,
"file_ext" => $this->file_ext,
"file_path" => $this->file_path,
"extensions" => $this->extensions,
"upload_dir" => $this->upload_dir,
"errors" => $this->errors,
"ok" => $this->ok,
"uuid" => $this->uuid
);
}
}