|
|
@ -7,6 +7,7 @@ |
|
|
|
|
|
|
|
|
|
|
|
namespace yii\web; |
|
|
|
namespace yii\web; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use yii\base\Object; |
|
|
|
use yii\helpers\Html; |
|
|
|
use yii\helpers\Html; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -17,49 +18,42 @@ use yii\helpers\Html; |
|
|
|
* You may also query other information about the file, including [[name]], |
|
|
|
* You may also query other information about the file, including [[name]], |
|
|
|
* [[tempName]], [[type]], [[size]] and [[error]]. |
|
|
|
* [[tempName]], [[type]], [[size]] and [[error]]. |
|
|
|
* |
|
|
|
* |
|
|
|
* @property integer $error The error code. This property is read-only. |
|
|
|
|
|
|
|
* @property boolean $hasError Whether there is an error with the uploaded file. Check [[error]] for detailed |
|
|
|
* @property boolean $hasError Whether there is an error with the uploaded file. Check [[error]] for detailed |
|
|
|
* error code information. This property is read-only. |
|
|
|
* error code information. This property is read-only. |
|
|
|
* @property string $name The original name of the file being uploaded. This property is read-only. |
|
|
|
|
|
|
|
* @property integer $size The actual size of the uploaded file in bytes. This property is read-only. |
|
|
|
|
|
|
|
* @property string $tempName The path of the uploaded file on the server. Note, this is a temporary file |
|
|
|
|
|
|
|
* which will be automatically deleted by PHP after the current request is processed. This property is read-only. |
|
|
|
|
|
|
|
* @property string $type The MIME-type of the uploaded file (such as "image/gif"). Since this MIME type is |
|
|
|
|
|
|
|
* not checked on the server side, do not take this value for granted. Instead, use [[FileHelper::getMimeType()]] |
|
|
|
|
|
|
|
* to determine the exact MIME type. This property is read-only. |
|
|
|
|
|
|
|
* |
|
|
|
* |
|
|
|
* @author Qiang Xue <qiang.xue@gmail.com> |
|
|
|
* @author Qiang Xue <qiang.xue@gmail.com> |
|
|
|
* @since 2.0 |
|
|
|
* @since 2.0 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
class UploadedFile extends \yii\base\Object |
|
|
|
class UploadedFile extends Object |
|
|
|
{ |
|
|
|
{ |
|
|
|
private static $_files; |
|
|
|
private static $_files; |
|
|
|
private $_name; |
|
|
|
|
|
|
|
private $_tempName; |
|
|
|
|
|
|
|
private $_type; |
|
|
|
|
|
|
|
private $_size; |
|
|
|
|
|
|
|
private $_error; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Constructor. |
|
|
|
* @var string the original name of the file being uploaded |
|
|
|
* Instead of using the constructor to create a new instance, |
|
|
|
|
|
|
|
* you should normally call [[getInstance()]] or [[getInstances()]] |
|
|
|
|
|
|
|
* to obtain new instances. |
|
|
|
|
|
|
|
* @param string $name the original name of the file being uploaded |
|
|
|
|
|
|
|
* @param string $tempName the path of the uploaded file on the server. |
|
|
|
|
|
|
|
* @param string $type the MIME-type of the uploaded file (such as "image/gif"). |
|
|
|
|
|
|
|
* @param integer $size the actual size of the uploaded file in bytes |
|
|
|
|
|
|
|
* @param integer $error the error code |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function __construct($name, $tempName, $type, $size, $error) |
|
|
|
public $name; |
|
|
|
{ |
|
|
|
/** |
|
|
|
$this->_name = $name; |
|
|
|
* @var string the path of the uploaded file on the server. |
|
|
|
$this->_tempName = $tempName; |
|
|
|
* Note, this is a temporary file which will be automatically deleted by PHP |
|
|
|
$this->_type = $type; |
|
|
|
* after the current request is processed. |
|
|
|
$this->_size = $size; |
|
|
|
*/ |
|
|
|
$this->_error = $error; |
|
|
|
public $tempName; |
|
|
|
} |
|
|
|
/** |
|
|
|
|
|
|
|
* @var string the MIME-type of the uploaded file (such as "image/gif"). |
|
|
|
|
|
|
|
* Since this MIME type is not checked on the server side, do not take this value for granted. |
|
|
|
|
|
|
|
* Instead, use [[FileHelper::getMimeType()]] to determine the exact MIME type. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public $type; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @var integer the actual size of the uploaded file in bytes |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public $size; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @var integer an error code describing the status of this file uploading. |
|
|
|
|
|
|
|
* @see http://www.php.net/manual/en/features.file-upload.errors.php |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public $error; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* String output. |
|
|
|
* String output. |
|
|
@ -69,7 +63,7 @@ class UploadedFile extends \yii\base\Object |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function __toString() |
|
|
|
public function __toString() |
|
|
|
{ |
|
|
|
{ |
|
|
|
return $this->_name; |
|
|
|
return $this->name; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -160,69 +154,23 @@ class UploadedFile extends \yii\base\Object |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function saveAs($file, $deleteTempFile = true) |
|
|
|
public function saveAs($file, $deleteTempFile = true) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if ($this->_error == UPLOAD_ERR_OK) { |
|
|
|
if ($this->error == UPLOAD_ERR_OK) { |
|
|
|
if ($deleteTempFile) { |
|
|
|
if ($deleteTempFile) { |
|
|
|
return move_uploaded_file($this->_tempName, $file); |
|
|
|
return move_uploaded_file($this->tempName, $file); |
|
|
|
} elseif (is_uploaded_file($this->_tempName)) { |
|
|
|
} elseif (is_uploaded_file($this->tempName)) { |
|
|
|
return copy($this->_tempName, $file); |
|
|
|
return copy($this->tempName, $file); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @return string the original name of the file being uploaded |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function getName() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return $this->_name; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @return string the path of the uploaded file on the server. |
|
|
|
|
|
|
|
* Note, this is a temporary file which will be automatically deleted by PHP |
|
|
|
|
|
|
|
* after the current request is processed. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function getTempName() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return $this->_tempName; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @return string the MIME-type of the uploaded file (such as "image/gif"). |
|
|
|
|
|
|
|
* Since this MIME type is not checked on the server side, do not take this value for granted. |
|
|
|
|
|
|
|
* Instead, use [[FileHelper::getMimeType()]] to determine the exact MIME type. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function getType() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return $this->_type; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @return integer the actual size of the uploaded file in bytes |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function getSize() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return $this->_size; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Returns an error code describing the status of this file uploading. |
|
|
|
|
|
|
|
* @return integer the error code |
|
|
|
|
|
|
|
* @see http://www.php.net/manual/en/features.file-upload.errors.php |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function getError() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return $this->_error; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @return boolean whether there is an error with the uploaded file. |
|
|
|
* @return boolean whether there is an error with the uploaded file. |
|
|
|
* Check [[error]] for detailed error code information. |
|
|
|
* Check [[error]] for detailed error code information. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function getHasError() |
|
|
|
public function getHasError() |
|
|
|
{ |
|
|
|
{ |
|
|
|
return $this->_error != UPLOAD_ERR_OK; |
|
|
|
return $this->error != UPLOAD_ERR_OK; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -258,7 +206,13 @@ class UploadedFile extends \yii\base\Object |
|
|
|
self::loadFilesRecursive($key . '[' . $i . ']', $name, $tempNames[$i], $types[$i], $sizes[$i], $errors[$i]); |
|
|
|
self::loadFilesRecursive($key . '[' . $i . ']', $name, $tempNames[$i], $types[$i], $sizes[$i], $errors[$i]); |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
self::$_files[$key] = new static($names, $tempNames, $types, $sizes, $errors); |
|
|
|
self::$_files[$key] = new static(array( |
|
|
|
|
|
|
|
'name' => $names, |
|
|
|
|
|
|
|
'tempName' => $tempNames, |
|
|
|
|
|
|
|
'type' => $types, |
|
|
|
|
|
|
|
'size' => $sizes, |
|
|
|
|
|
|
|
'error' => $errors, |
|
|
|
|
|
|
|
)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|