Question

I'm having trouble when renaming a series of files to upload using Zend_File_Transfer_Adapter_Http. At some point in the code, I ran into a strange syntax. Say you have a series of validations on the multiple files that I'm trying to upload. I'm using a foreach loop to iterate over the files after calling the class method getFileInfo(). So basically I do a check to see if each file doesn't pass validation and afterwards, if they do pass validation, I'm trying to run the following code:

else {
$reconocido=TRUE;
$sin_espacios=str_replace(' ','_', $nombre_file, $renombrado);
$reconocido=FALSE;
$uploader->addValidator('Extension', FALSE, 'gif, jpg, png, pdf');
if($uploader->isValid($archivo)) {
$reconocido=TRUE;   
} else {
$mime = $uploader->getMimeType($archivo);
$aceptados = array('jpg'=>'image/jpeg', 'png'=>'image/png', 'gif'=>'image/gif',    'pdf'=>'application/pdf');
$clave = array_search($mime, $aceptados);
if(!$clave) {
$messages[]="Tipo de archivo no reconocido.";
} else {
$sin_espacios = "$sin_espacios.$clave";
$reconocido=TRUE;
$renombrado=TRUE;     
}
    }   

if($reconocido) {
$existe = scandir($destino);
if(in_array($sin_espacios, $existe)) {
$punto = strrpos($sin_espacios, '.');
$base = substr($sin_espacios, 0, $punto);
$extension = substr($sin_espacios, $punto);
$i = 1;
do {
$sin_espacios=$base.'_'.$i++.$extension;    
} while (in_array($sin_espacios, $existe));
$renombrado=TRUE;
}
$uploader->clearValidators();   
$uploader->addFilter('Rename', array('target'=>$sin_espacios, $info['tmp_name']));
$exito=$uploader->receive($archivo);

The problem I seem to have is with the line just before the last line, namely $uploader->addFilter('Rename', array('target'=>$sin_espacios, $info['tmp_name'])); because it seems an odd syntax (why would you include a comma inside a key value in an array? And as it happens, running the code does upload the files, and renames them alright, but I keep having the following warnings:

Warning: Illegal string offset 'source' in C:\Zend_FW\ZendFramework-1.11.12-minimal\library\Zend\Filter\File\Rename.php on line 145

Warning: Illegal string offset 'target' in C:\Zend_FW\ZendFramework-1.11.12-minimal\library\Zend\Filter\File\Rename.php on line 145 And also: File 'C:\Windows\Temp\php97CA.tmp' could not be renamed. It already exists. Forgive me if I'm asking something that should be obvious. The fact is the documentation I have states that the syntax I'm talking about, is correct, but in the ZendFramework site I haven't found anything like it. So what would be the correct syntax for doing this? I also realize that using the ZendFramework is quite complex, perhaps, for my present level, but it has been working for me until I tried to do multiple uploads. I read a very thorough explanation about file uploads with Zend here: PHP: How to rename a file uploaded with Zend_Form_Element_File? But I'm not too worried about files being overwritten because this is an administrative section of the webpage. Please I'll be thankful for any idea.

In order to be more precise I suspect the problem is that the Filter cannot recognize the new name I'm trying to apply. I've used the 'target' option of the Rename filter before without trouble, and it being, according to the manual: target: The new directory, or filename of the file. I guess I'm doing it right. And though I wonder if I should use the complete path to the file here, I still cannot see why the syntax I mentioned before should be used, but then again what is the right syntax, huh?

Was it helpful?

Solution

I suspect your instinct is correct and the syntax is almost correct. The api for addFilter() is:

addFilter($filter, $options = null, $files = null): Adds the given filter to the filter stack (optionally only to the file(s) specified). $filter may be either an actual filter instance, or a short name specifying the filter type (e.g., 'Rename').

I'm pretty sure the offending line should read:

$uploader->addFilter('Rename', array('target'=>$sin_espacios, 'source'=>$info['tmp_name'], 'overwrite' => true));

this according to the docblock for Zend_Filter_File_Rename

   /**
     * Class constructor
     *
     * Options argument may be either a string, a Zend_Config object, or an array.
     * If an array or Zend_Config object, it accepts the following keys:
     * 'source'    => Source filename or directory which will be renamed
     * 'target'    => Target filename or directory, the new name of the sourcefile
     * 'overwrite' => Shall existing files be overwritten ?
     *
     * @param  string|array $options Target file or directory to be renamed
     * @param  string $target Source filename or directory (deprecated)
     * @param  bool $overwrite Should existing files be overwritten (deprecated)
     * @return void
     */

I hope this will clear the warnings, I think it should.

OTHER TIPS

Actually your original syntax was correct, except you missed the file name as a required parameter. Omitting the key as 'source' as well as overwrite option is OK, you just need the file in addFilter (requires only in case of multi-file upload).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top