00001 <?php
00008 class UploadField extends AbstractField
00009 {
00010 protected $dir = '';
00011
00017 public function __set($name, $value)
00018 {
00019 if($name == 'dir')
00020 $this->dir = $value;
00021 else
00022 parent::__set($name, $value);
00023 }
00024
00028 public function drawInput()
00029 {
00030 $maxSize = min($this->returnBytes(ini_get('upload_max_filesize')), $this->returnBytes(ini_get('post_max_size')));
00031 echo <<<EOHTML
00032 <input type="file" name="$this->name" size="30">
00033 <input type="hidden" name="MAX_FILE_SIZE" value="$maxSize"/>
00034 EOHTML;
00035 }
00036
00037
00042 public function validate()
00043 {
00044
00045 if($_FILES[$this->name]['error'])
00046 {
00047 if($_FILES[$this->name]['error'] == 4 && $this->required)
00048 {
00049 $this->hasError = true;
00050 $this->errorMessage = "There was an error with the $this->title field.";
00051 return;
00052 }
00053 }
00054
00055
00056 if($this->required && !$_FILES[$this->name])
00057 {
00058 $this->errorMessage = "The $this->title field is required.";
00059 $this->hasError = true;
00060 return;
00061 }
00062
00063
00064 $this->setFromRequest();
00065
00066
00067 if(!$this->dir)
00068 throw new Exception("UploadField destination directory is not set (Pass the 'dir' option when you add the field).");
00069 elseif(!is_writable($this->dir))
00070 throw new Exception("UploadField directory ($this->dir) is not writable.");
00073
00074 move_uploaded_file($_FILES[$this->name]['tmp_name'], $this->dir . '/' . $_FILES[$this->name]['name']);
00075
00076 $this->hasError = false;
00077 }
00078
00079
00083 public function setFromRequest()
00084 {
00085 $this->value = $_FILES[$this->name]['name'];
00086 }
00087
00088
00095 protected function returnBytes($val)
00096 {
00097 $val = trim($val);
00098 $last = strtolower($val{strlen($val)-1});
00099
00100 switch($last)
00101 {
00102
00103 case 'g':
00104 $val *= 1024;
00105 case 'm':
00106 $val *= 1024;
00107 case 'k':
00108 $val *= 1024;
00109 }
00110
00111 return $val;
00112 }
00113 }
00114 ?>