Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/form-file-validation' into develop
Browse files Browse the repository at this point in the history
File validation for forms, with PRG support

Close zendframework/zendframework#2439
  • Loading branch information
Show file tree
Hide file tree
Showing 4 changed files with 544 additions and 1 deletion.
51 changes: 51 additions & 0 deletions src/File/RenameUpload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Filter
*/

namespace Zend\Filter\File;

use Zend\Filter\Exception;
use Zend\Stdlib\ErrorHandler;

/**
* @category Zend
* @package Zend_Filter
*/
class RenameUpload extends Rename
{
/**
* Defined by Zend\Filter\Filter
*
* Renames the file $value to the new name set before
* Returns the file $value, removing all but digit characters
*
* @param string $value Full path of file to change
* @throws Exception\RuntimeException
* @return string The new filename which has been set, or false when there were errors
*/
public function filter($value)
{
$file = $this->getNewName($value, true);
if (is_string($file)) {
return $file;
}

ErrorHandler::start();
$result = move_uploaded_file($file['source'], $file['target']);
$warningException = ErrorHandler::stop();
if (!$result || null !== $warningException) {
throw new Exception\RuntimeException(
sprintf("File '%s' could not be renamed. An error occurred while processing the file.", $value),
0, $warningException
);
}

return $file['target'];
}
}
1 change: 1 addition & 0 deletions src/FilterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class FilterPluginManager extends AbstractPluginManager
'fileencrypt' => 'Zend\Filter\File\Encrypt',
'filelowercase' => 'Zend\Filter\File\LowerCase',
'filerename' => 'Zend\Filter\File\Rename',
'filerenameupload' => 'Zend\Filter\File\RenameUpload',
'fileuppercase' => 'Zend\Filter\File\UpperCase',
'htmlentities' => 'Zend\Filter\HtmlEntities',
'inflector' => 'Zend\Filter\Inflector',
Expand Down
2 changes: 1 addition & 1 deletion test/File/RenameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ public function testAddFileWithInvalidOption()
/**
* @return void
*/
public function testInvalidContruction()
public function testInvalidConstruction()
{
$this->setExpectedException('\Zend\Filter\Exception\InvalidArgumentException', 'Invalid options');
$filter = new FileRename(1234);
Expand Down
Loading

0 comments on commit 1bd0805

Please sign in to comment.