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/SBogers60/agrimac.nl/workbench/komma/kms/src/Komma/Kms/Files/FileService.php
<?php


namespace Komma\Kms\Files;



use Carbon\Carbon;

class FileService
{
    /**
     * @param $value
     * @return array|bool|mixed|string
     * @throws \Exception
     */
    public function upload($value, $subFolder)
    {
        if( ! $value) return false;

        // Get files or return if no value files found
        $files = (is_array($value)) ? $value : json_decode($value);
        if( ! $files) return false;

        // Loop through files
        foreach ($files as $key => $file)
        {
            if (isset($file->tmpName))
            {
                //Upload File
                $file = \Input::file($file->tmpName);


                // Find original name
                $originalFilename = $file->getClientOriginalName();
                // Find paths
                $fullPath = \Config::get('kms::paths.full_path_files');
                $path = \Config::get('kms::paths.path_files');

                // Add sub folders
                if($subFolder != null)
                {
                    $fullPath .= '/' . $subFolder;
                    $path .= '/' . $subFolder;
                }

                // Create filename
                $filename = time().'_'.helper_space_to_underscore($originalFilename);

                // Save file to the server
                $file->move($fullPath, $filename);

                // Add file to array for return
                $files[$key]->name = $originalFilename;
                $files[$key]->path = $path .'/'.$filename;
            }

            // Delete files on update
            if (isset($file->id) && isset($file->delete))
            {
                // Find the file model
                $file = \Komma\Kms\Files\Model\File::Find($file->id);

                // Delete from server
                $this->deleteFileFromServer($file);
            }
        }

        return $files;
    }

    public function delete($value)
    {
        $files = (is_array($value)) ? $value : json_decode($value);

        foreach($files as $file)
        {
            // Delete form server
            $this->deleteFileFromServer($file);

            // Delete from database
            if(isset($file->id)) \Komma\Kms\Files\Model\File::destroy($file->id);
        }
    }

    /*
     * Delete a file from the server
     */
    protected function deleteFileFromServer($file)
    {
        if(isset($file->path) && ! empty($file->path) && \File::exists(public_path().$file->path))
            \File::delete(public_path() . $file->path);
    }

    /**
     * Remove old files from the server
     *
     * @param $old_files
     */
    public function cleanUp($old_files)
    {
        if (empty($old_files)) return;
        foreach ($old_files as $old_file) {
            if (empty($old_file)) return;
            $path = $this->oneslash(public_path() . $old_file);

            if (file_exists($path)) {
                unlink($path);
            }
        }
    }


    public function oneslash($string)
    {
        return str_replace('\/', '/', $string);
    }
}