From: avl Date: Wed, 10 Sep 2008 15:16:58 +0000 (+0300) Subject: Rewrite Upload File Names X-Git-Tag: 0.5~497 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=96a22c61e60e215633ba5a2d8981ee934984d9a2;p=ipf.git Rewrite Upload File Names --- diff --git a/ipf/form/field/file.php b/ipf/form/field/file.php index d7cb36a..646c6eb 100644 --- a/ipf/form/field/file.php +++ b/ipf/form/field/file.php @@ -5,13 +5,12 @@ class IPF_Form_Field_File extends IPF_Form_Field public $widget = 'IPF_Form_Widget_FileInput'; public $move_function = 'IPF_Form_Field_moveToUploadFolder'; public $remove_function = 'IPF_Form_Field_removeFile'; - public $max_size = 2097152; // 2MB + public $max_size = 8388608; // 8MB public $move_function_params = array(); function clean($value) { - if ($value['remove']==1){ - //print_r($value); + if ($value['remove']===true){ IPF::loadFunction($this->remove_function); return call_user_func($this->remove_function, $value['data']); } @@ -61,11 +60,11 @@ class IPF_Form_Field_File extends IPF_Form_Field function IPF_Form_Field_moveToUploadFolder($value, $params=array()) { - $name = IPF_Utils::cleanFileName($value['name']); $upload_path = IPF::get('upload_path', '/tmp'); if (isset($params['upload_path'])) { $upload_path = $params['upload_path']; } + $name = IPF_Utils::cleanFileName($value['name'], $upload_path); $dest = $upload_path.DIRECTORY_SEPARATOR.$name; if (!move_uploaded_file($value['tmp_name'], $dest)) { throw new IPF_Exception_Form(__('An error occured when upload the file. Please try to send the file again.')); diff --git a/ipf/form/widget/fileinput.php b/ipf/form/widget/fileinput.php index c28c8b3..75dfdf1 100644 --- a/ipf/form/widget/fileinput.php +++ b/ipf/form/widget/fileinput.php @@ -19,7 +19,10 @@ class IPF_Form_Widget_FileInput extends IPF_Form_Widget_Input public function valueFromFormData($name, $data) { if (isset($data[$name])) { - $remove = (int)$data[$name.'_remove']; + $remove = false; + if (isset($data[$name.'_remove'])) + if ($data[$name.'_remove']==1) + $remove = true; $res = array('data'=>$data[$name], 'remove'=>$remove); return $res; } diff --git a/ipf/utils.php b/ipf/utils.php index 5979aa9..94305ec 100644 --- a/ipf/utils.php +++ b/ipf/utils.php @@ -43,10 +43,27 @@ class IPF_Utils { return $mysize; } - static function cleanFileName($name) + static function cleanFileName($name, $path) { $name = mb_strtolower($name, 'UTF-8'); - return mb_ereg_replace("/\015\012|\015|\012|\s|[^A-Za-z0-9\.\-\_]/", '_', $name); + $name = mb_ereg_replace("/\015\012|\015|\012|\s|[^A-Za-z0-9\.\-\_]/", '_', $name); + + while(file_exists($path.$name)){ + $pathinfo = pathinfo($name); + $filename = $pathinfo['filename']; + $split = split('_', $filename); + $n = count($split); + if ($n<2){ + $filename .= '_2'; + } + else{ + $x = (int)$split[$n-1]; + $y = $x+1; + $filename = str_replace('_'.$x, '_'.$y, $filename); + } + $name = $filename.'.'.$pathinfo['extension']; + } + return $name; } static function isValidUrl($url)