ZF-6576: Zend_Filter_File_Rename getNewName parameters (Bit of Zend_Transfer_Adapter_Http too)

Description

In 1.8.0 Beta you would get the original filename in the getNewName function, in the 1.8.0 release it was changed to the temporary name.

So currently we have extended the Zend_Filter_File_Rename because we want to apply our own naming scheme to the uploaded files, But we still need to know the original files extension to do this. Before we got the original filename and could use that, now we get the temporary name, which on Windows includes a .tmp extension and on linux doesn't have an extension at all.

At the point where the function get's called (in the Zend_Transfer_Adapter_Http line 163) the $this->_files property hasn't been set yet. We could do it in a hackish way and get the information from $_FILES, but I much rather have all the information available to the function at that point.

Comments

According to API the getNetName() returns the new name of a filtered file without moving it. It's an undocumented internal method and a preparation for an improvement of this filter.

Another point is, that Zend_Filter_File_Rename does not know about original, new or old names. This informations are stored within Zend_File_Transfer.

To get the old name simply call getFileName() BEFORE calling getValues() or receive(). Calling it afterwards return the new set filename.

The filter is definetly the wrong point to get file informations.

Never mind.. I'm an idiot !

Hello Thomas,

I Am facing the same issue as Haro will trying to move from ZF 1.6 to 1.8 I had extended Zend_Filter_File_Rename because I wanted my own naming scheme.

But now Zend_Transfer_Adapter_Http line 163 call the Zend_Filter_File_Rename::getNewName() with the temp name. Why such change ?

And I 'cant use the solution you describe above because I'm using Zend_Form and everything is seamless handle by Zend_Form

So right now I'm stuck. The only solution I found is patching Zend_Transfer_Adapter_Http. which is not nice. What do you suggest ?

Thanks

Ok,

I found that even if my filter is named Mmx_Filter_File_AutoRename, it get catch by Zend_Transfer_Adapter_Http::getFilter('Rename') on line 161 Or if it wasn't catch $filename = $rename->getNewName($content['tmp_name']); wouldn't be called directly and my AutoRename will work has usual.

I see 2 solutions :

1) So what about to be more restrictive on the regex line 1468 in Zend_Transfer_Adapter_Abstract::_getFilterIdentifier($name) so Mmx_Filter_File_Rename would match and not Mmx_Filter_File_AutoRename

this change will do the trick


      foreach (array_keys($this->_filters) as $test) {
            if (preg_match('/_' . preg_quote($name) . '$/i', $test)) {
                return $test;
            }
        }

And it could also avoid some weird thing

2) after get Zend_Transfer_Adapter_Http::getFilter('Rename') called make sure the filter is Zend_Filter_File_Rename like so


           $rename   = $this->getFilter('Rename');
                if ($rename !== null && 'Zend_Filter_File_Rename' == get_class($rename)) {
                    $filename = $rename->getNewName($content['tmp_name']);
                    $key      = array_search(get_class($rename), $this->_files[$file]['filters']);
                    unset($this->_files[$file]['filters'][$key]);
                }

When you have the same problem as Harro then also the solution is the same.

To get the original name you have to store it before calling the rename filter. Before the rename filter is called the original name is returned by getFileName, afterwards the new filename is returned.

According to Zend_Filter_File_Rename: This feature was integrated with 1.8 to allow the rename filter to be integrated with Zend_File_Transfer. Otherwise Zend_File_Transfer would have no way to know how the rename filter wants to rename the files. There were several bugs related to this.

PS: Zend_Transfer does not exist.

Extending Zend_Filter_File_Rename correct would work. But patching Zend_File_Transfer_Adapter_Http to 1.6 again is the wrong way. This introduces again several bugs which have been fixed.

{quote} When you have the same problem as Harro then also the solution is the same. {quote} Well, my question is different (how about with zend_form ?) so i guess the solution could be different.

{quote} To get the original name you have to store it before calling the rename filter. Before the rename filter is called the original name is returned by getFileName, afterwards the new filename is returned. {quote}

So I need to do all kind of tricking stuff to have it working with Zend_Form and it is not what I want.

{quote} Extending Zend_Filter_File_Rename correct would work. {quote} Yes it would work but right now it won't if the filter name end by "Rename" (AutoRename, MyCustomeRename, etc...) have you consider one of my fix in my previous message for that ?

Thanks