Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Zend\Filter\File\RenameUpload - Added possibility to maintain original file extension #3990

Closed
wants to merge 1 commit into from

2 participants

@robertboloc

When using the Zend\Filter\File\RenameUpload filter on a file upload (that can have any extension) the extension is lost if the target does not specify it.

Added a new option (use_upload_extension) that, when specified, maintains the original extension.

Example :

If the user uploads a .png image test.png :

<?php
$filter = new RenameUpload(array(
    'target'  => pictures,
    'use_upload_extension'  => true,
    'randomize'  => true,
 ));

The filter will return something like pictures_ad321e2r5ty5s.png

Works with randomize on and off.

@weierophinney weierophinney was assigned
@weierophinney weierophinney referenced this pull request from a commit
@weierophinney weierophinney [#3990] CS fixes
- elseif clause
49007d3
@weierophinney

As this adds to the public API, merged to develop for release with 2.2.0.

@robertboloc robertboloc referenced this pull request in zendframework/zf2-documentation
Closed

Added docs zendframework/zf2#3990 #874

@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney [#3990] CS fixes
- elseif clause
14ba589
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'feature/3990' into develop
Close #3990
ea993d5
@samsonasik samsonasik referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-filter
@weierophinney weierophinney Merge pull request zendframework/zf2#3990 from robertboloc/feature-re…
…name-upload-maintain-extension

Zend\Filter\File\RenameUpload - Added possibility to maintain original file extension
edb9d4b
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-filter
@weierophinney weierophinney [zendframework/zf2#3990] CS fixes
- elseif clause
9e9bd7b
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-filter
@weierophinney weierophinney Merge branch 'feature/3990' into develop 78201e3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
52 library/Zend/Filter/File/RenameUpload.php
@@ -19,10 +19,11 @@ class RenameUpload extends AbstractFilter
* @var array
*/
protected $options = array(
- 'target' => null,
- 'use_upload_name' => false,
- 'overwrite' => false,
- 'randomize' => false,
+ 'target' => null,
+ 'use_upload_name' => false,
+ 'use_upload_extension' => false,
+ 'overwrite' => false,
+ 'randomize' => false,
);
/**
@@ -92,6 +93,25 @@ public function getUseUploadName()
}
/**
+ * @param boolean $flag When true, this filter will use the original file
+ * extension for the target filename
+ * @return RenameUpload
+ */
+ public function setUseUploadExtension($flag = true)
+ {
+ $this->options['use_upload_extension'] = (boolean) $flag;
+ return $this;
+ }
+
+ /**
+ * @return boolean
+ */
+ public function getUseUploadExtension()
+ {
+ return $this->options['use_upload_extension'];
+ }
+
+ /**
* @param boolean $flag Shall existing files be overwritten?
* @return RenameUpload
*/
@@ -242,12 +262,19 @@ protected function getFinalTarget($uploadData)
$targetFile = basename($uploadData['name']);
} elseif (!is_dir($target)) {
$targetFile = basename($target);
+ if ($this->getUseUploadExtension() && !$this->getRandomize()) {
+ $targetInfo = pathinfo($targetFile);
+ $sourceinfo = pathinfo($uploadData['name']);
+ if (isset($sourceinfo['extension'])) {
+ $targetFile = $targetInfo['filename'] . '.' . $sourceinfo['extension'];
+ }
+ }
} else {
$targetFile = basename($source);
}
if ($this->getRandomize()) {
- $targetFile = $this->applyRandomToFilename($targetFile);
+ $targetFile = $this->applyRandomToFilename($uploadData['name'], $targetFile);
}
return $targetDir . $targetFile;
@@ -257,13 +284,20 @@ protected function getFinalTarget($uploadData)
* @param string $filename
* @return string
*/
- protected function applyRandomToFilename($filename)
+ protected function applyRandomToFilename($source,$filename)
{
$info = pathinfo($filename);
$filename = $info['filename'] . uniqid('_');
- if (isset($info['extension'])) {
- $filename .= '.' . $info['extension'];
+
+ $sourceinfo = pathinfo($source);
+
+ $extension = '';
+ if ($this->getUseUploadExtension() === true && isset($sourceinfo['extension'])) {
+ $extension .= '.' . $sourceinfo['extension'];
+ } else if (isset($info['extension'])) {
+ $extension .= '.' . $info['extension'];
}
- return $filename;
+
+ return $filename . $extension;
}
}
View
34 tests/ZendTest/Filter/File/RenameUploadTest.php
@@ -258,6 +258,40 @@ public function testGetRandomizedFile()
$this->assertRegExp('#' . $fileNoExt . '_.{13}\.xml#', $filter($this->_oldFile));
}
+ public function testGetFileWithOriginalExtension()
+ {
+ $fileNoExt = $this->_filesPath . '/newfile';
+ $filter = new RenameUploadMock(array(
+ 'target' => $this->_newFile,
+ 'use_upload_extension' => true,
+ 'randomize' => false,
+ ));
+
+ $oldFilePathInfo = pathinfo($this->_oldFile);
+
+ $this->assertRegExp(
+ '#' . $fileNoExt . '.'.$oldFilePathInfo['extension'].'#',
+ $filter($this->_oldFile)
+ );
+ }
+
+ public function testGetRandomizedFileWithOriginalExtension()
+ {
+ $fileNoExt = $this->_filesPath . '/newfile';
+ $filter = new RenameUploadMock(array(
+ 'target' => $this->_newFile,
+ 'use_upload_extension' => true,
+ 'randomize' => true,
+ ));
+
+ $oldFilePathInfo = pathinfo($this->_oldFile);
+
+ $this->assertRegExp(
+ '#' . $fileNoExt . '_.{13}\.'.$oldFilePathInfo['extension'].'#',
+ $filter($this->_oldFile)
+ );
+ }
+
/**
* @return void
*/
Something went wrong with that request. Please try again.