PHP code example of alex-kalanis / kw_files

1. Go to this page and download the library: Download alex-kalanis/kw_files library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alex-kalanis / kw_files example snippets


// DI-like
return function (array $params): \kalanis\kw_files\Access\CompositeAdapter {
    return (new \kalanis\kw_files\Access\Factory())->getClass($params);
}

// Classes
use \kalanis\kw_files\Access;

class FileOperations
{
    public function __construct(
        // ...
        protected readonly Access\CompositeAdapter $files,
        // ...
    ) {}

    public function upload(string $name, string $content): bool
    {
        $objName = (new \kalanis\kw_paths\ArrayPath())->setString($name);
        if (!$this->files->isDir($objName->getArrayDirectory())) {
            return false;
        }
        if ($this->files->exists($objName->getArray())) {
            return false;
        }
        return $this->files->saveFile($objName->getArray(), $content);
    }

    public function move(string $from, string $to): bool
    {
        $arrFrom = (new \kalanis\kw_paths\ArrayPath())->setString($from)->getArray();
        $objTo = (new \kalanis\kw_paths\ArrayPath())->setString($to);
        if (!$this->files->exists($arrFrom)) {
            return false;
        }
        if (!$this->files->exists($objTo->getArrayDirectory())) {
            return false;
        }
        if ($this->files->exists($objTo->getArray())) {
            return false;
        }
        return $this->files->isDir($arrFrom)
            ? $this->files->moveDir($arrFrom, $objTo->getArray())
            : $this->files->moveFile($arrFrom, $objTo->getArray())
        ;
    }
}