Skip to content

Commit ba64bc4

Browse files
Update class.uploader.php
1 parent 7f0bc7e commit ba64bc4

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

src/class.uploader.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
# Title [PHP] Uploader
55
# Author: CreativeDream
66
# 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
99
# Purpose: Validate, Remove, Upload, Download files to server.
1010
# Information: Please don't forget to check your php.ini file for "upload_max_filesize", "post_max_size", "max_file_uploads"
1111
#
@@ -197,6 +197,8 @@ private function prepareFiles(){
197197
if($validate){
198198
$files = array();
199199
$removed_files = $this->removeFiles();
200+
$isAddMoreMode = count(preg_grep('/^(\d+)\:\/\/(.*)/i', $removed_files)) > 0;
201+
$addMoreMatches = array();
200202

201203
for($i = 0; $i < count($field['name']); $i++){
202204

@@ -225,7 +227,16 @@ private function prepareFiles(){
225227
$metas['replaced'] = file_exists($metas['file']);
226228
$metas['date'] = date('r');
227229

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'])){
229240
if($this->options['perms']) @chmod($metas['file'], $this->options['perms']);
230241

231242
$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){
270281
* @return array
271282
*/
272283
private function removeFiles(){
273-
$input = null;
274284
$removed_files = array();
275-
if($this->options['removeFiles'] === true){
285+
if($this->options['removeFiles'] !== false){
276286
foreach($_POST as $key=>$value){
277287
preg_match((is_string($this->options['removeFiles']) ? $this->options['removeFiles'] : '/jfiler-items-exclude-'.$this->field['Field_Name'].'-(\d+)/'), $key, $matches);
278-
279288
if(isset($matches) && !empty($matches)){
280289
$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;
282293
}
283294
}
284-
}elseif(is_string($this->options['removeFiles'])){
285-
$input = $this->options['removeFiles'];
286295
}
287296

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-
}
293297
return $removed_files;
294298
}
295299

@@ -402,7 +406,7 @@ private function generateFileName($conf, $file, $skip_replace_check = false){
402406
* Check if string is a valid json
403407
* @return boolean
404408
*/
405-
function isJson($string) {
409+
private function isJson($string) {
406410
json_decode($string);
407411
return (json_last_error() == JSON_ERROR_NONE);
408412
}

0 commit comments

Comments
 (0)