HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers64/klimroosbudel.nl/wwwroot/kms/lib/files/file_uploader.class.php
<?php
/**
 * file_uploader.class.php
 * Created by Komma Mediadesign.
 * Author: mike
 * Date: 4/16/13
 */
class File_Uploader
{
    /*
     * File to upload
     */
    private $_file;

    /*
     * Target where we will upload the file
     */
    private $_uploadTarget;

    /*
     * Extensions that we are allowed to upload.
     * Default set to jpg, jpeg, gif and png.
     */
    private $_allowedExt = ['jpg', 'jpeg', 'gif', 'png'];

    /*
     * Maximum size of a file that we upload.
     * Default set to 10mb.
     */
    private $_maxSize = 10000000;

    /*
     * Errors that occur during upload.
     */
    private $_errors = [];

    public function __construct($file)
    {
        $this->_file = $file;
        $this->_uploadTarget = DOCUMENT_UPLOADS_ROOT;
    }

    /*

        GET / SET

    */

    /*
     * Set
     */
    public function set($prop, $val)
    {
        if (! empty($prop)) {
            $this->{$prop} = $val;
        }
    }

    /*
     * Set
     */
    public function get($prop)
    {
        if (! empty($prop)) {
            return $this->{$prop};
        }

        return false;
    }

    /**
     * Returns the extension of a given fileString
     *
     * @param string
     * @return string
     */
    public function getExt($input)
    {
        $temp = explode('.', $input);
        $fileExt = strtolower($temp[count($temp) - 1]);

        return $fileExt;
    }

    /*
     * Convert bytes to megabytes
     */
    private function mb($input)
    {
        return ceil($input / 1000000).'mb';
    }

    /*

        IMAGE HANDLING

    */

    /**
     * Uploads the image
     *
     * @return bool
     */
    public function upload()
    {
        $path = $this->_uploadTarget.urlencode($this->_file['name']);

        if (copy($this->_file['tmp_name'], $path)) {
            return urlencode($this->_file['name']);
        }

        return false;
    }

    /*

        VALIDATION

    */

    /**
     * Validates the image
     *
     * @return bool
     */
    public function validate()
    {
        if ($this->checkSize() && $this->checkExt()) {
            return true;
        }

        return false;
    }

    /**
     * Checks if the file size is valid
     *
     * @return bool
     */
    private function checkSize()
    {
        if ($this->_file['size'] < $this->_maxSize) {
            return true;
        }

        // Set error message
        $this->_errors[] = 'File size too big, this is '.$this->mb($this->_file['size']).' and max is '.$this->mb($this->_maxSize);

        return false;
    }

    /**
     * Returns a string containing the extension of the file
     *
     * @return bool
     */
    private function checkExt()
    {
        $fileExt = $this->getExt($this->_file['name']);
        if (in_array($fileExt, $this->_allowedExt)) {
            return true;
        }

        // Set error message
        $msg = 'Invalid extension, '.$this->_file['name'].' has "'.$fileExt.'". Allowed are ';
        foreach ($this->_allowedExt as $ext) {
            $msg .= $ext.', ';
        }
        $msg = substr($msg, 0, -2);

        $this->_errors[] = $msg;

        return false;
    }

    /**
     * Checks whether the upload directory is writable
     *
     * @param
     * @return bool
     */
    private function isWritable()
    {
        if (is_writable($this->_uploadTarget)) {
            return true;
        } else {
            $this->_errors[] = 'Upload target "'.$this->_uploadTarget.'" is not writable';

            return false;
        }
    }
}