|
4 | 4 | # Title [PHP] Uploader |
5 | 5 | # Author: CreativeDream |
6 | 6 | # Website: https://github.com/CreativeDream/php-uploader |
7 | | -# Version: 0.3 |
8 | | -# Date: 17-Aug-2016 |
| 7 | +# Version: 0.4 |
| 8 | +# Date: 14-Sep-2016 |
9 | 9 | # Purpose: Validate, Remove, Upload, Download files to server. |
10 | 10 | # Information: Please don't forget to check your php.ini file for "upload_max_filesize", "post_max_size", "max_file_uploads" |
11 | 11 | # |
@@ -197,6 +197,8 @@ private function prepareFiles(){ |
197 | 197 | if($validate){ |
198 | 198 | $files = array(); |
199 | 199 | $removed_files = $this->removeFiles(); |
| 200 | + $isAddMoreMode = count(preg_grep('/^(\d+)\:\/\/(.*)/i', $removed_files)) > 0; |
| 201 | + $addMoreMatches = array(); |
200 | 202 |
|
201 | 203 | for($i = 0; $i < count($field['name']); $i++){ |
202 | 204 |
|
@@ -225,7 +227,16 @@ private function prepareFiles(){ |
225 | 227 | $metas['replaced'] = file_exists($metas['file']); |
226 | 228 | $metas['date'] = date('r'); |
227 | 229 |
|
228 | | - if(!in_array($field['name'][$i], $removed_files) && $this->validate(array_merge($metas, array('index'=>$i, 'tmp'=>$tmp_name))) && $this->uploadFile($tmp_name, $metas['file'])){ |
| 230 | + $is_file_removed = in_array($field['name'][$i], $removed_files); |
| 231 | + if($isAddMoreMode){ |
| 232 | + $addMoreMatches[$field['name'][$i]][] = $i; |
| 233 | + $matches = preg_grep('/^'.(count($addMoreMatches[$field['name'][$i]])-1).'\:\/\/'.$field['name'][$i].'/i', $removed_files); |
| 234 | + if(count($matches) == 1) { |
| 235 | + $is_file_removed = true; |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + if(!$is_file_removed && $this->validate(array_merge($metas, array('index'=>$i, 'tmp'=>$tmp_name))) && $this->uploadFile($tmp_name, $metas['file'])){ |
229 | 240 | if($this->options['perms']) @chmod($metas['file'], $this->options['perms']); |
230 | 241 |
|
231 | 242 | $custom = $this->_onUpload($metas, $this->field); if($custom && is_array($custom)) $metas = array_merge($custom, $metas); |
@@ -270,26 +281,19 @@ private function uploadFile($source, $destination){ |
270 | 281 | * @return array |
271 | 282 | */ |
272 | 283 | private function removeFiles(){ |
273 | | - $input = null; |
274 | 284 | $removed_files = array(); |
275 | | - if($this->options['removeFiles'] === true){ |
| 285 | + if($this->options['removeFiles'] !== false){ |
276 | 286 | foreach($_POST as $key=>$value){ |
277 | 287 | preg_match((is_string($this->options['removeFiles']) ? $this->options['removeFiles'] : '/jfiler-items-exclude-'.$this->field['Field_Name'].'-(\d+)/'), $key, $matches); |
278 | | - |
279 | 288 | if(isset($matches) && !empty($matches)){ |
280 | 289 | $input = $_POST[$matches[0]]; |
281 | | - break; |
| 290 | + if($this->isJson($input)) $removed_files = json_decode($input, true); |
| 291 | + |
| 292 | + $custom = $this->_onRemove($removed_files, $this->field); if($custom && is_array($custom)) $removed_files = $custom; |
282 | 293 | } |
283 | 294 | } |
284 | | - }elseif(is_string($this->options['removeFiles'])){ |
285 | | - $input = $this->options['removeFiles']; |
286 | 295 | } |
287 | 296 |
|
288 | | - if($input != null){ |
289 | | - if($this->isJson($input)) $removed_files = json_decode($input, true); |
290 | | - |
291 | | - $custom = $this->_onRemove($removed_files, $this->field); if($custom && is_array($custom)) $removed_files = $custom; |
292 | | - } |
293 | 297 | return $removed_files; |
294 | 298 | } |
295 | 299 |
|
@@ -402,7 +406,7 @@ private function generateFileName($conf, $file, $skip_replace_check = false){ |
402 | 406 | * Check if string is a valid json |
403 | 407 | * @return boolean |
404 | 408 | */ |
405 | | - function isJson($string) { |
| 409 | + private function isJson($string) { |
406 | 410 | json_decode($string); |
407 | 411 | return (json_last_error() == JSON_ERROR_NONE); |
408 | 412 | } |
|
0 commit comments