|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- <?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,
- ];
- }
- }
- }
|