Web::receive PHP Method

receive() public method

Receive file(s) from HTTP client
public receive ( $func = NULL, $overwrite = FALSE, $slug = TRUE ) : array | boolean
$func callback
$overwrite bool
$slug callback|bool
return array | boolean
    function receive($func = NULL, $overwrite = FALSE, $slug = TRUE)
    {
        $fw = Base::instance();
        $dir = $fw->get('UPLOADS');
        if (!is_dir($dir)) {
            mkdir($dir, Base::MODE, TRUE);
        }
        if ($fw->get('VERB') == 'PUT') {
            $tmp = $fw->get('TEMP') . $fw->get('SEED') . '.' . $fw->hash(uniqid());
            if (!$fw->get('RAW')) {
                $fw->write($tmp, $fw->get('BODY'));
            } else {
                $src = @fopen('php://input', 'r');
                $dst = @fopen($tmp, 'w');
                if (!$src || !$dst) {
                    return FALSE;
                }
                while (!feof($src) && ($info = stream_get_meta_data($src)) && !$info['timed_out'] && ($str = fgets($src, 4096))) {
                    fputs($dst, $str, strlen($str));
                }
                fclose($dst);
                fclose($src);
            }
            $base = basename($fw->get('URI'));
            $file = ['name' => $dir . ($slug && preg_match('/(.+?)(\\.\\w+)?$/', $base, $parts) ? is_callable($slug) ? $slug($base) : $this->slug($parts[1]) . (isset($parts[2]) ? $parts[2] : '') : $base), 'tmp_name' => $tmp, 'type' => $this->mime($base), 'size' => filesize($tmp)];
            return (!file_exists($file['name']) || $overwrite) && (!$func || $fw->call($func, [$file]) !== FALSE) && rename($tmp, $file['name']);
        }
        $fetch = function ($arr) use(&$fetch) {
            if (!is_array($arr)) {
                return [$arr];
            }
            $data = [];
            foreach ($arr as $k => $sub) {
                $data = array_merge($data, $fetch($sub));
            }
            return $data;
        };
        $out = [];
        foreach ($_FILES as $name => $item) {
            $files = [];
            foreach ($item as $k => $mix) {
                foreach ($fetch($mix) as $i => $val) {
                    $files[$i][$k] = $val;
                }
            }
            foreach ($files as $file) {
                if (empty($file['name'])) {
                    continue;
                }
                $base = basename($file['name']);
                $file['name'] = $dir . ($slug && preg_match('/(.+?)(\\.\\w+)?$/', $base, $parts) ? is_callable($slug) ? $slug($base, $name) : $this->slug($parts[1]) . (isset($parts[2]) ? $parts[2] : '') : $base);
                $out[$file['name']] = !$file['error'] && (!file_exists($file['name']) || $overwrite) && (!$func || $fw->call($func, [$file, $name]) !== FALSE) && move_uploaded_file($file['tmp_name'], $file['name']);
            }
        }
        return $out;
    }