Zend Framework comes with a set of classes for filtering file contents as well as performing other actions, such as file renaming.
Note
All of the File Filter Classes’ filter() methods support both a file path string or a $_FILES array as the supplied argument. When a $_FILES array is passed in, the tmp_name is used for the file path.
TODO
TODO
TODO
Zend\Filter\File\Rename can be used to rename a file and/or move a file to a new path.
Supported Options
The following set of options are supported:
Target filename or directory, the new name of the source file.
Source filename or directory which will be renamed.
Used to match the filtered file with an options set.
Shall existing files be overwritten?
If the file is unable to be moved into the target path, a Zend\Filter\Exception\RuntimeException will be thrown.
Shall target files have a random postfix attached? The random postfix will be a uniqid('_') after the file name and before the extension.
For example, "file.txt" will be randomized to "file_4b3403665fea6.txt"
An array of option sets is also supported, where a single Rename filter instance can filter several files using different options. The options used for the filtered file will be matched from the source option in the options set.
Usage Examples
Move all filtered files to a different directory:
1 2 3 4 | // 'target' option is assumed if param is a string
$filter = \Zend\Filter\File\Rename("/tmp/");
echo $filter->filter("./myfile.txt");
// File has been moved to "/tmp/myfile.txt"
|
Rename all filtered files to a new name:
1 2 3 | $filter = \Zend\Filter\File\Rename("/tmp/newfile.txt");
echo $filter->filter("./myfile.txt");
// File has been renamed to "/tmp/newfile.txt"
|
Move to a new path and randomize file names:
1 2 3 4 5 6 | $filter = \Zend\Filter\File\Rename(array(
"target" => "/tmp/newfile.txt",
"randomize" => true,
));
echo $filter->filter("./myfile.txt");
// File has been renamed to "/tmp/newfile_4b3403665fea6.txt"
|
Configure different options for several possible source files:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | $filter = \Zend\Filter\File\Rename(array(
array(
"source" => "fileA.txt"
"target" => "/dest1/newfileA.txt",
"overwrite" => true,
),
array(
"source" => "fileB.txt"
"target" => "/dest2/newfileB.txt",
"randomize" => true,
),
));
echo $filter->filter("fileA.txt");
// File has been renamed to "/dest1/newfileA.txt"
echo $filter->filter("fileB.txt");
// File has been renamed to "/dest2/newfileB_4b3403665fea6.txt"
|
Public Methods
The specific public methods for the Rename filter, besides the common filter() method, are as follows:
Returns the files to rename and their new name and location
Return type: | array |
---|
Sets the file options for renaming. Removes any previously set file options.
Parameters: | $options – See Supported Options section for more information. |
---|
Adds file options for renaming to the current list of file options.
Parameters: | $options – See Supported Options section for more information. |
---|
Zend\Filter\File\RenameUpload can be used to rename or move an uploaded file to a new path.
Supported Options
The following set of options are supported:
Target directory or full filename path.
Shall existing files be overwritten?
If the file is unable to be moved into the target path, a Zend\Filter\Exception\RuntimeException will be thrown.
Shall target files have a random postfix attached? The random postfix will be a uniqid('_') after the file name and before the extension.
For example, "file.txt" will be randomized to "file_4b3403665fea6.txt"
When true, this filter will use the $_FILES[‘name’] as the target filename. Otherwise, the default target rules and the $_FILES['tmp_name'] will be used.
When true, the uploaded file will maintains its original extension if not specified.
For example, if the uploaded file is "file.txt" and the target is something like "mynewfile", the upload will be renamed to "mynewfile.txt".
Warning
Be very careful when using the use_upload_name option. For instance, extremely bad things could happen if you were to allow uploaded .php files (or other CGI files) to be moved into the DocumentRoot.
It is generally a better idea to supply an internal filename to avoid security risks.
RenameUpload does not support an array of options like the``Rename`` filter. When filtering HTML5 file uploads with the multiple attribute set, all files will be filtered with the same option settings.
Usage Examples
Move all filtered files to a different directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | use Zend\Http\PhpEnvironment\Request;
$request = new Request();
$files = $request->getFiles();
// i.e. $files['my-upload']['tmp_name'] === '/tmp/php5Wx0aJ'
// i.e. $files['my-upload']['name'] === 'myfile.txt'
// 'target' option is assumed if param is a string
$filter = \Zend\Filter\File\RenameUpload("./data/uploads/");
echo $filter->filter($files['my-upload']);
// File has been moved to "./data/uploads/php5Wx0aJ"
// ... or retain the uploaded file name
$filter->setUseUploadName(true);
echo $filter->filter($files['my-upload']);
// File has been moved to "./data/uploads/myfile.txt"
|
Rename all filtered files to a new name:
1 2 3 4 5 6 7 8 9 | use Zend\Http\PhpEnvironment\Request;
$request = new Request();
$files = $request->getFiles();
// i.e. $files['my-upload']['tmp_name'] === '/tmp/php5Wx0aJ'
$filter = \Zend\Filter\File\Rename("./data/uploads/newfile.txt");
echo $filter->filter($files['my-upload']);
// File has been renamed to "./data/uploads/newfile.txt"
|
Move to a new path and randomize file names:
1 2 3 4 5 6 7 8 9 10 11 12 | use Zend\Http\PhpEnvironment\Request;
$request = new Request();
$files = $request->getFiles();
// i.e. $files['my-upload']['tmp_name'] === '/tmp/php5Wx0aJ'
$filter = \Zend\Filter\File\Rename(array(
"target" => "./data/uploads/newfile.txt",
"randomize" => true,
));
echo $filter->filter($files['my-upload']);
// File has been renamed to "./data/uploads/newfile_4b3403665fea6.txt"
|
TODO
The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.