Symfony Upload File Name


Problem while uploading file in admin generator ?

The extension is replaced by .exe for .ods files ? or for MIME application/octet-stream ?

Just want to keep the name of you files when uploading ?

This is your solution:

Put this mySfValidatorFile in your root/lib folder.

<?php

/**
 * mySfValidatorFile validates an uploaded file.
 *
 * Fix problem with strange extension file upload in Admin Generator
 *
 * @package    lib
 * @subpackage not necessary
 * @author     Fabien Vauthey fabien . vauthey . gmail . com
 */
class mySfValidatorFile extends sfValidatorFile
{
   
    protected function configure($options = array(), $messages = array())
        {
        parent::configure();
        $this->addOption('validated_file_class', 'sfValidatedFileFab');
       
        }
}

class sfValidatedFileFab extends sfValidatedFile {
   
    public function generateFilename()
        {
        return $this->getOriginalName().'_'.sha1($this->getOriginalName().rand(11111, 99999)).$this->getOriginalExtension();
        }
}

And clear your cache (symfony cc).

Finally use it like that:

$this->widgetSchema['path'] = new sfWidgetFormInputFileEditable(array(
            'label' => 'File',
            'file_src'  => sfConfig::get('app_path_relative_files_dir').'/'.$this->getObject()->getPath(),    
            'is_image'  => false,
            'edit_mode' => !$this->isNew(),
            'template'  => '<div>Link to the file : <b> <a href="%file%" target="_blank">Click Here to Download "'.$this->getObject()->getName().'"</a> </b><br />%input%<br />%delete% Just remove the current file</div>',
        ));

Don’t forget to replace file_src by yours ;)

Et voilaaaa !

Inspired by: http://tigor.com.ua/blog/2008/12/10/symfony-12-fix-problem-with-upload-flv-in-admin/
(yes of course I read Russian ^^)

Fab
Latest posts by Fab (see all)

About Fab

Solutions Architect, I build great workflows for the news and media production industries. I play with data too.

Leave a comment

Your email address will not be published. Required fields are marked *