@@ -19,10 +19,11 @@ class RenameUpload extends AbstractFilter
1919 * @var array
2020 */
2121 protected $ options = array (
22- 'target ' => null ,
23- 'use_upload_name ' => false ,
24- 'overwrite ' => false ,
25- 'randomize ' => false ,
22+ 'target ' => null ,
23+ 'use_upload_name ' => false ,
24+ 'use_upload_extension ' => false ,
25+ 'overwrite ' => false ,
26+ 'randomize ' => false ,
2627 );
2728
2829 /**
@@ -72,55 +73,74 @@ public function getTarget()
7273 }
7374
7475 /**
75- * @param boolean $flag When true, this filter will use the $_FILES['name']
76+ * @param bool $flag When true, this filter will use the $_FILES['name']
7677 * as the target filename.
7778 * Otherwise, it uses the default 'target' rules.
7879 * @return RenameUpload
7980 */
8081 public function setUseUploadName ($ flag = true )
8182 {
82- $ this ->options ['use_upload_name ' ] = (boolean ) $ flag ;
83+ $ this ->options ['use_upload_name ' ] = (bool ) $ flag ;
8384 return $ this ;
8485 }
8586
8687 /**
87- * @return boolean
88+ * @return bool
8889 */
8990 public function getUseUploadName ()
9091 {
9192 return $ this ->options ['use_upload_name ' ];
9293 }
9394
9495 /**
95- * @param boolean $flag Shall existing files be overwritten?
96+ * @param bool $flag When true, this filter will use the original file
97+ * extension for the target filename
98+ * @return RenameUpload
99+ */
100+ public function setUseUploadExtension ($ flag = true )
101+ {
102+ $ this ->options ['use_upload_extension ' ] = (bool ) $ flag ;
103+ return $ this ;
104+ }
105+
106+ /**
107+ * @return bool
108+ */
109+ public function getUseUploadExtension ()
110+ {
111+ return $ this ->options ['use_upload_extension ' ];
112+ }
113+
114+ /**
115+ * @param bool $flag Shall existing files be overwritten?
96116 * @return RenameUpload
97117 */
98118 public function setOverwrite ($ flag = true )
99119 {
100- $ this ->options ['overwrite ' ] = (boolean ) $ flag ;
120+ $ this ->options ['overwrite ' ] = (bool ) $ flag ;
101121 return $ this ;
102122 }
103123
104124 /**
105- * @return boolean
125+ * @return bool
106126 */
107127 public function getOverwrite ()
108128 {
109129 return $ this ->options ['overwrite ' ];
110130 }
111131
112132 /**
113- * @param boolean $flag Shall target files have a random postfix attached?
133+ * @param bool $flag Shall target files have a random postfix attached?
114134 * @return RenameUpload
115135 */
116136 public function setRandomize ($ flag = true )
117137 {
118- $ this ->options ['randomize ' ] = (boolean ) $ flag ;
138+ $ this ->options ['randomize ' ] = (bool ) $ flag ;
119139 return $ this ;
120140 }
121141
122142 /**
123- * @return boolean
143+ * @return bool
124144 */
125145 public function getRandomize ()
126146 {
@@ -179,7 +199,7 @@ public function filter($value)
179199 * @param string $sourceFile Source file path
180200 * @param string $targetFile Target file path
181201 * @throws \Zend\Filter\Exception\RuntimeException
182- * @return boolean
202+ * @return bool
183203 */
184204 protected function moveUploadedFile ($ sourceFile , $ targetFile )
185205 {
@@ -242,12 +262,19 @@ protected function getFinalTarget($uploadData)
242262 $ targetFile = basename ($ uploadData ['name ' ]);
243263 } elseif (!is_dir ($ target )) {
244264 $ targetFile = basename ($ target );
265+ if ($ this ->getUseUploadExtension () && !$ this ->getRandomize ()) {
266+ $ targetInfo = pathinfo ($ targetFile );
267+ $ sourceinfo = pathinfo ($ uploadData ['name ' ]);
268+ if (isset ($ sourceinfo ['extension ' ])) {
269+ $ targetFile = $ targetInfo ['filename ' ] . '. ' . $ sourceinfo ['extension ' ];
270+ }
271+ }
245272 } else {
246273 $ targetFile = basename ($ source );
247274 }
248275
249276 if ($ this ->getRandomize ()) {
250- $ targetFile = $ this ->applyRandomToFilename ($ targetFile );
277+ $ targetFile = $ this ->applyRandomToFilename ($ uploadData [ ' name ' ], $ targetFile );
251278 }
252279
253280 return $ targetDir . $ targetFile ;
@@ -257,13 +284,20 @@ protected function getFinalTarget($uploadData)
257284 * @param string $filename
258285 * @return string
259286 */
260- protected function applyRandomToFilename ($ filename )
287+ protected function applyRandomToFilename ($ source , $ filename )
261288 {
262289 $ info = pathinfo ($ filename );
263290 $ filename = $ info ['filename ' ] . uniqid ('_ ' );
264- if (isset ($ info ['extension ' ])) {
265- $ filename .= '. ' . $ info ['extension ' ];
291+
292+ $ sourceinfo = pathinfo ($ source );
293+
294+ $ extension = '' ;
295+ if ($ this ->getUseUploadExtension () === true && isset ($ sourceinfo ['extension ' ])) {
296+ $ extension .= '. ' . $ sourceinfo ['extension ' ];
297+ } elseif (isset ($ info ['extension ' ])) {
298+ $ extension .= '. ' . $ info ['extension ' ];
266299 }
267- return $ filename ;
300+
301+ return $ filename . $ extension ;
268302 }
269303}
0 commit comments