|
- <?php
-
-
- namespace yii\web;
-
- use yii\base\Object;
- use yii\helpers\Html;
-
-
- class UploadedFile extends Object
- {
-
-
- public $name;
-
-
- public $tempName;
-
-
- public $type;
-
-
- public $size;
-
-
- public $error;
-
- private static $_files;
-
-
-
-
- public function __toString()
- {
- return $this->name;
- }
-
-
-
- public static function getInstance($model, $attribute)
- {
- $name = Html::getInputName($model, $attribute);
- return static::getInstanceByName($name);
- }
-
-
-
- public static function getInstances($model, $attribute)
- {
- $name = Html::getInputName($model, $attribute);
- return static::getInstancesByName($name);
- }
-
-
-
- public static function getInstanceByName($name)
- {
- $files = self::loadFiles();
- return isset($files[$name]) ? new static($files[$name]) : null;
- }
-
-
-
- public static function getInstancesByName($name)
- {
- $files = self::loadFiles();
- if (isset($files[$name])) {
- return [new static($files[$name])];
- }
- $results = [];
- foreach ($files as $key => $file) {
- if (strpos($key, "{$name}[") === 0) {
- $results[] = new static($file);
- }
- }
- return $results;
- }
-
-
-
- public static function reset()
- {
- self::$_files = null;
- }
-
-
-
- public function saveAs($file, $deleteTempFile = true)
- {
- if ($this->error == UPLOAD_ERR_OK) {
- if ($deleteTempFile) {
- return move_uploaded_file($this->tempName, $file);
- } elseif (is_uploaded_file($this->tempName)) {
- return copy($this->tempName, $file);
- }
- }
- return false;
- }
-
-
-
- public function getBaseName()
- {
-
- $pathInfo = pathinfo('_' . $this->name, PATHINFO_FILENAME);
- return mb_substr($pathInfo, 1, mb_strlen($pathInfo, '8bit'), '8bit');
- }
-
-
-
- public function getExtension()
- {
- return strtolower(pathinfo($this->name, PATHINFO_EXTENSION));
- }
-
-
-
- public function getHasError()
- {
- return $this->error != UPLOAD_ERR_OK;
- }
-
-
-
- private static function loadFiles()
- {
- if (self::$_files === null) {
- self::$_files = [];
- if (isset($_FILES) && is_array($_FILES)) {
- foreach ($_FILES as $class => $info) {
- self::loadFilesRecursive($class, $info['name'], $info['tmp_name'], $info['type'], $info['size'], $info['error']);
- }
- }
- }
- return self::$_files;
- }
-
-
-
- private static function loadFilesRecursive($key, $names, $tempNames, $types, $sizes, $errors)
- {
- if (is_array($names)) {
- foreach ($names as $i => $name) {
- self::loadFilesRecursive($key . '[' . $i . ']', $name, $tempNames[$i], $types[$i], $sizes[$i], $errors[$i]);
- }
- } elseif ((int)$errors !== UPLOAD_ERR_NO_FILE) {
- self::$_files[$key] = [
- 'name' => $names,
- 'tempName' => $tempNames,
- 'type' => $types,
- 'size' => $sizes,
- 'error' => $errors,
- ];
- }
- }
- }
|