<?php

require_once(root . 'admin/classes/resize-class.php');
ini_set('upload_max_filesize', '10M');
$path      = upload_folder;
$errors    = [];
$status    = [];
$file_name = $files['name'];
$file_size = $files['size'];
$file_tmp  = $files['tmp_name'];
$file_type = $files['type'];
$file_ext  = strtolower(end(explode('.', $files['name'])));

$expensions = ['jpeg','jpg','png','doc','pdf','xls','xl'];

if (in_array($file_ext, $expensions) == false) {
    $errors[] = 'extension not allowed, please choose a JPEG or PNG file.';
}

if ($file_size > 2097152) {
    $errors[] = 'File size must be excately 2 MB';
}
$file_name          = time() . '.' . $file_ext;
$status['filename'] = $file_name;

if (empty($errors) == true) {
    $target           = $path . $file_name;
    $status['target'] = $target;
    move_uploaded_file($file_tmp, $path . $file_name);

    if ($id) {
        $imageArray['image'] = $file_name;
        saveArray($table, $imageArray, $id);
    }

    if ($thumb_width) {
        $UPLOAD_PATH = $path . '/thumb/';
        $img         = $target;
        $resizeObj   = new resize($img);
        $resizeObj -> resizeImage($thumb_width, $thumb_height, 'crop');
        $imagename2 = $file_name;
        $resizeObj -> saveImage($UPLOAD_PATH . $imagename2, 100);
    }
} else {
    $status = 0;
}
