Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00017 class PrimitiveFile extends RangedPrimitive
00018 {
00019 private $originalName = null;
00020 private $mimeType = null;
00021
00022 private $allowedMimeTypes = array();
00023 private $checkUploaded = true;
00024
00025 public function getOriginalName()
00026 {
00027 return $this->originalName;
00028 }
00029
00030 public function getMimeType()
00031 {
00032 return $this->mimeType;
00033 }
00034
00038 public function clean()
00039 {
00040 $this->originalName = null;
00041 $this->mimeType = null;
00042
00043 return parent::clean();
00044 }
00045
00050 public function setAllowedMimeTypes($mimes)
00051 {
00052 Assert::isArray($mimes);
00053
00054 $this->allowedMimeTypes = $mimes;
00055
00056 return $this;
00057 }
00058
00063 public function addAllowedMimeType($mime)
00064 {
00065 Assert::isString($mime);
00066
00067 $this->allowedMimeTypes[] = $mime;
00068
00069 return $this;
00070 }
00071
00072 public function getAllowedMimeTypes()
00073 {
00074 return $this->allowedMimeTypes;
00075 }
00076
00077 public function isAllowedMimeType()
00078 {
00079 if (count($this->allowedMimeTypes) > 0) {
00080 return in_array($this->mimeType, $this->allowedMimeTypes);
00081 } else
00082 return true;
00083 }
00084
00085 public function copyTo($path, $name)
00086 {
00087 return $this->copyToPath($path.$name);
00088 }
00089
00090 public function copyToPath($path)
00091 {
00092 if (is_readable($this->value) && is_writable(dirname($path))) {
00093 if ($this->checkUploaded) {
00094 return move_uploaded_file($this->value, $path);
00095 } else {
00096 return rename($this->value, $path);
00097 }
00098 } else
00099 throw new WrongArgumentException(
00100 "can not move '{$this->value}' to '{$path}'"
00101 );
00102 }
00103
00104 public function import($scope)
00105 {
00106 if (
00107 !BasePrimitive::import($scope)
00108 || !is_array($scope[$this->name])
00109 || (
00110 isset($scope[$this->name], $scope[$this->name]['error'])
00111 && $scope[$this->name]['error'] == UPLOAD_ERR_NO_FILE
00112 )
00113 )
00114 return null;
00115
00116 if (isset($scope[$this->name]['tmp_name']))
00117 $file = $scope[$this->name]['tmp_name'];
00118 else
00119 return false;
00120
00121 if (is_readable($file) && $this->checkUploaded($file))
00122 $size = filesize($file);
00123 else
00124 return false;
00125
00126 $this->mimeType = $scope[$this->name]['type'];
00127
00128 if (!$this->isAllowedMimeType())
00129 return false;
00130
00131 if (
00132 isset($scope[$this->name])
00133 && !($this->max && ($size > $this->max))
00134 && !($this->min && ($size < $this->min))
00135 ) {
00136 $this->value = $scope[$this->name]['tmp_name'];
00137 $this->originalName = $scope[$this->name]['name'];
00138
00139 return true;
00140 }
00141
00142 return false;
00143 }
00144
00145 public function exportValue()
00146 {
00147 throw new UnimplementedFeatureException();
00148 }
00149
00153 public function enableCheckUploaded()
00154 {
00155 $this->checkUploaded = true;
00156
00157 return $this;
00158 }
00159
00163 public function disableCheckUploaded()
00164 {
00165 $this->checkUploaded = false;
00166
00167 return $this;
00168 }
00169
00170 public function isCheckUploaded()
00171 {
00172 return $this->checkUploaded;
00173 }
00174
00175 private function checkUploaded($file)
00176 {
00177 return !$this->checkUploaded || is_uploaded_file($file);
00178 }
00179 }
00180 ?>