Issues

ZF-10046: Rename Filter not working correctly if abbreviated form used

Description

Please notice that there seems to be a heavy bug if you try to use the "rename" filter on each file before "receive" (to prevent problems with files being uploaded that have identical the same name (but don't have to be the same file)):

Register a filter object instead of the 3-parameter-style to make it work, as following:


<?php
[...]

//Dateien individuell umbenennen, sonst gibt es Probleme mit gleichnamigen Dateien.
        $files = $parFileTransfer->getFileInfo();
        //Achtung, Dateien sind an dieser Stelle noch temporär!
        $i = 0;
        foreach($files as $key => $fileInfo){   
            if(!$fileInfo['name']==''){
                //Jede Datei in ein eigenes, zufälliges Verzeichnis verschieben um Namenskonflikte zu verhindern.
                $parFileTransfer->addFilter(
                    new Zend_Filter_File_Rename(
                        array(  'source'    =>  $fileInfo['tmp_name'],
                                'target'    =>  $i.$key)
                    )
                );
                $i++;
            }
        }

[...]
?>

<-- Result absolutely OK!


The following did not identify the files correctly, even if it should! Result was, that all files were named the same (identity seems to be set wrong in rename). The rename filters were wrongly applied on all files though.

DO NOT USE (this part):


<?php
[...]
$parFileTransfer->addFilter('Rename', $i.$key, $fileInfo['tmp_name']);
[...]
?>

Comments

Changed code stlyle

Well, as I found out, there is another problem:

Zend_File_Transfer_Adapter_Abstract can only handle one filter of each type, which is a problem if you have to act on file basis, not on general upload basis:

In "addFilter" this is being done by: "$this->_filters[$class] = $filter;"

But it should be a construction like: "$this->_filters[$class][] = $filter;" to act on file basis.

The current implementation only uses the last added filter, which is not the true meaning of "addFilter", but of "setFilter" or something like this.

As described above you have to fix this with no doubt, because else you are never able to upload files named same!!! (not even using this workaround!!) So I expect this as HIGHLY CRITICAL!

Changed priority because this bug affects general functionality of uploading same named files and the code doesn't work as expected/documented!

Degrading priority.

The given example implies that the used code is wrong.

Using the temporary name instead of the file element does not work when you use the code given within the manual. This works only when you have another workflow which is not officially documented.

You did not point out when $parFileTransfer should be. You did not point out how you create the form. You did not point out how you receive the files.

The actual example can not be verified. Please add some code which can be reproduced to see where you have problems.

Firstofall, thank you for your quick reply.

Please give me a working example, how to handle a file upload with files having the same name. In my oppinion it is just not possible using Zend_File_Transfer and that's really bad, isn't it?

If you were able to add several rename filters on file basis, not just one on upload basis, you could use my workaround, but because only the last rename filter can be used (override in ZF as described above) this doesn't work.

I think it is easier if you try to find a solution for the "same file name"-problem yourself, perhaps you will have the same problems. If not, it would be nice if you could add your solution to the documentation and post ist here, because this is a major problem in my eyes.

I think you are misunderstanding what issues are.

Creating an issue does not mean that you get your code fixed and developed an solution because you don't know how it works.

It is not my work to show you how you can reproduce your problem.

Closing as not reproduceable with the given example

So just two last questions, which I hope you will answer: Don't you think that it is bad, that with this Component you are not able to upload two files with the same name at once?

Don't you think that it is bad, that there can only be one filter for each type even if the function is called addFilter (which is misleading)?

And please don't say I don't know how it works, thats not true. If you had a look yourself, you would know.

following code should work for multiple files upload.


 $target_path =  '/var/www/uploads/';
            $adapter = new Zend_File_Transfer_Adapter_Http();
            $adapter->addValidator('Size', false, 102400);
            $adapter->addValidator('IsImage', false, array(
                    'gif',
                    'jpeg',
                    'png'
            ));

            $files = $adapter->getFileInfo();
            foreach ($files as $file => $info) {
                if ($info['name'] != '') {
                    if ($adapter->isValid($file)) {
                        //generate new filename , you can using random hash filename, output 32acb74d.jpg
                        $newFileName = $this->_getNewFileName($info);
                        $adapter->clearFilters()->addFilter(new Zend_Filter_File_Rename( array(
                                'source' => $info['tmp_name'],
                                'target' => $target_path . $newFileName,
                                'overwrite' => TRUE
                        )));
                        if (!$adapter->receive($file)) {
                            $this->_uploadFailed($adapter);
                        }
                        $this->_addEnvPhoto($this->_getDisplayUrl() . '/' . $newFileName, $info);
                    }
                }
            }
            Zend_Debug::dump($adapter->getFileInfo());
            Zend_Debug::dump($adapter->getFilters());
            exit();

This issue like to say :

when using Zend_File_Transfer handle multiple files uploads, we do not know how to move uploads files to right directory.

by document, seems we could using Zend_Filter_File_Rename , but it seems not working correctly.

would you please show me a short code snippet , handle multiple files upload, move upload files to right target directory? (using ZendFramwork, Zend_File, Zend_Filter and etc)