Changeset 1658
- Timestamp:
- 01/02/08 00:34:15 (6 months ago)
- Files:
-
- trunk/modules/forge/controllers/forge_demo.php (modified) (1 diff)
- trunk/modules/forge/i18n/en_US/forge.php (modified) (1 diff)
- trunk/modules/forge/libraries/Form_Upload.php (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/modules/forge/controllers/forge_demo.php
r1657 r1658 46 46 $form = new Forge; 47 47 $form->input('hello')->label(TRUE); 48 $form->upload('file')->label(TRUE)->rules('required| allow[jpg,png,gif]|size[2M]');48 $form->upload('file')->label(TRUE)->rules('required|size[100KB]|allow[jpg,png,gif]'); 49 49 $form->submit('Upload'); 50 50 trunk/modules/forge/i18n/en_US/forge.php
r1522 r1658 4 4 ( 5 5 'invalid_input' => 'Invalid input type requested: %s', 6 'address_unit' => 'Unit #',7 'address_city' => 'City',8 'address_state' => 'State',9 'address_zip' => 'Zip'10 6 ); trunk/modules/forge/libraries/Form_Upload.php
r1657 r1658 11 11 protected $protect = array('type', 'label', 'value'); 12 12 13 // Upload data 13 14 protected $upload; 15 16 // Upload directory 14 17 protected $directory; 15 18 … … 49 52 // Make sure the upload director is valid and writable 50 53 if ($dir === '/' OR ! is_dir($dir) OR ! is_writable($dir)) 51 throw new Kohana_Exception(' forge.upload.unwritable', $dir);54 throw new Kohana_Exception('upload.not_writable', $dir); 52 55 53 56 $this->directory = $dir; … … 59 62 empty($this->directory) and $this->directory(); 60 63 61 if ($status = parent::validate()) 64 // By default, there is no uploaded file 65 $filename = ''; 66 67 if ($status = parent::validate() AND $this->upload['error'] === UPLOAD_ERR_OK) 62 68 { 63 // No filename means an invalid upload64 $filename = '';69 // Set the filename to the original name 70 $filename = $this->upload['name']; 65 71 66 if ( $this->upload['error'] === UPLOAD_ERR_OK)72 if (Config::item('upload.remove_spaces')) 67 73 { 68 // Set the filename to the original name 69 $filename = $this->upload['name']; 70 71 if (Config::item('upload.remove_spaces')) 72 { 73 // Remove spaces, due to global upload configuration 74 $filename = preg_replace('/\s+/', '_', $this->data['value']); 75 } 76 77 // Move the uploaded file to the upload directory 78 move_uploaded_file($this->upload['tmp_name'], $filename = $this->directory.$filename); 74 // Remove spaces, due to global upload configuration 75 $filename = preg_replace('/\s+/', '_', $this->data['value']); 79 76 } 80 77 78 // Move the uploaded file to the upload directory 79 move_uploaded_file($this->upload['tmp_name'], $filename = $this->directory.$filename); 80 } 81 82 if ( ! empty($_POST[$this->data['name']])) 83 { 81 84 // Reset the POST value to the new filename 82 85 $this->data['value'] = $_POST[$this->data['name']] = $filename; … … 96 99 public function rule_allow() 97 100 { 98 if (empty($this->upload['tmp_name']) )101 if (empty($this->upload['tmp_name']) OR count($types = func_get_args()) == 0) 99 102 return; 100 103 … … 122 125 $allow = FALSE; 123 126 124 foreach ( func_get_args()as $type)127 foreach ($types as $type) 125 128 { 126 129 if (in_array($mime, Config::item('mimes.'.$type))) … … 134 137 if ($allow === FALSE) 135 138 { 136 $this->errors[' allow'] = TRUE;139 $this->errors['invalid_type'] = TRUE; 137 140 } 138 141 } … … 142 145 $bytes = (int) $size; 143 146 144 switch (substr($size, - 1))147 switch (substr($size, -2)) 145 148 { 146 case 'G': $bytes *= 1024; 147 case 'M': $bytes *= 1024; 148 default: $bytes *= 1024; 149 case 'GB': $bytes *= 1024; 150 case 'MB': $bytes *= 1024; 151 case 'KB': $bytes *= 1024; 152 default: break; 149 153 } 150 154 151 155 if (empty($this->upload['size']) OR $this->upload['size'] > $bytes) 152 156 { 153 $this->errors[' size'] = $size;157 $this->errors['max_size'] = array($size); 154 158 } 155 159 } 156 160 157 p ublic function html()161 protected function html_element() 158 162 { 159 163 return form::upload($this->data);
