/** * Saves the uploaded file. * Note that this method uses php's move_uploaded_file() method. If the target file `$file` * already exists, it will be overwritten. * * @param string $file the file path used to save the uploaded file * @param boolean $deleteTempFile whether to delete the temporary file after saving. * If true, you will not be able to save the uploaded file again in the current request. * @param bool $createDir * @return bool true whether the file is saved successfully * @throws \Exception * @see error */ public function saveAs($file, $deleteTempFile = true, $createDir = false) { if ($this->error !== UPLOAD_ERR_OK) { return false; } $file = Alias::getAlias($file); if ($this->calculatePathname instanceof \Closure) { $file = call_user_func($this->calculatePathname, $this, $file, $this->adapter); } if ($createDir) { FileHelper::createDirectory(dirname($file)); } if ($deleteTempFile) { return move_uploaded_file($this->tempName, $file); } elseif (is_uploaded_file($this->tempName)) { return copy($this->tempName, $file); } return false; }
protected function defaultHandlers() { $path = $path = Alias::getAlias('@runtime/logs'); FileHelper::createDirectory($path); $paths = [self::DEBUG => "{$path}/debug.log", self::INFO => "{$path}/info.log", self::NOTICE => "{$path}/error.log", self::WARNING => "{$path}/error.log", self::ERROR => "{$path}/error.log", self::CRITICAL => "{$path}/error.log", self::ALERT => "{$path}/error.log", self::EMERGENCY => "{$path}/error.log"]; $formatter = new LineFormatter("[%datetime%]\t%level_name%\t%extra.hash%\t%message%\t%extra.user_id%\t%extra.user_ip%\t%extra.user_agent%\n"); $this->logger->pushProcessor(function ($record) { $record['extra']['hash'] = substr(md5($record['message']), -6); $record['extra']['user_agent'] = strip_tags($_SERVER['HTTP_USER_AGENT']); $record['extra']['user_ip'] = filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP); $record['extra']['user_id'] = isset($_SESSION['user']['id']) ? $_SESSION['user']['id'] : 'NULL'; return $record; }); $handlers = []; foreach ($paths as $level => $path) { $handlers[$level] = (new StreamHandler($path, $level, false))->setFormatter($formatter); } return $handlers; }