Pop\File\File::setFile PHP Method

setFile() protected method

Set the file and its properties.
protected setFile ( string $file, array $types = null ) : void
$file string
$types array
return void
    protected function setFile($file, $types = null)
    {
        // Set file object properties.
        $file_parts = pathinfo($file);
        if (null !== $types) {
            $this->allowed = $types;
        }
        $this->fullpath = $file;
        $this->dir = $file_parts['dirname'];
        $this->basename = $file_parts['basename'];
        $this->filename = $file_parts['filename'];
        $this->ext = isset($file_parts['extension']) ? $file_parts['extension'] : null;
        // Check if the file exists, and set the size and permissions accordingly.
        if (file_exists($file)) {
            if (is_dir($file)) {
                throw new Exception('The file passed is a directory.');
            }
            $this->size = filesize($file);
        } else {
            $this->size = 0;
        }
        // Check to see if the file is an accepted file format.
        if (null !== $this->allowed && null !== $this->ext && count($this->allowed) > 0 && !array_key_exists(strtolower($this->ext), $this->allowed)) {
            throw new Exception('Error: The file type ' . strtoupper($this->ext) . ' is not an accepted file format.');
        }
        // Set the mime type of the file.
        $this->mime = null !== $this->ext && count($this->allowed) > 0 && null !== $this->allowed ? $this->allowed[strtolower($this->ext)] : 'text/plain';
    }