symfony1 - How to let the user choose the upload directory? -
i have form used upload images in blog engine. files uploaded web/uploads, i'd add "choice" widget let users pick list of folders, instance 'photos', 'cliparts', 'logos'.
here's form
class imageform extends baseform { public function configure() { $this->widgetschema->setnameformat('image[%s]'); $this->setwidget('file', new sfwidgetforminputfileeditable( array( 'edit_mode'=>false, 'with_delete' => false, 'file_src' => '', ) )); $this->setvalidator('file', new mysfvalidatorfile( array( 'max_size' => 500000, 'mime_types' => 'web_images', 'path' => 'uploads', 'required' => true ) )); $this->setwidget('folder', new sfwidgetformchoice(array( 'expanded' => false, 'multiple' => false, 'choices' => array('photos', 'cliparts', 'logos') ) )); $this->setvalidator('folder', new sfvalidatorchoice(array( 'choices' => array(0,1,2) ))); } }
and here action :
public function executeajout(sfwebrequest $request) { $this->form = new imageform(); if ($request->ismethod('post')) { $this->form->bind( $request->getparameter($this->form->getname()), $request->getfiles($this->form->getname()) ); if ($this->form->isvalid()) { $this->form->getvalue('file')->save(); $this->image = $this->form->getvalue('file'); } }
i'm using custom file validator :
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(); } }
so how tell file upload widget save image in different folder ?
you can concatenate directory names said ('photos', 'cliparts', 'logos') sf_upload_dir code below shows, need create directories of course.
$this->validatorschema['file'] = new sfvalidatorfile( array('path' => sfconfig::get('sf_upload_dir' . '/' . $path) ));
also, can have directories detailes in app.yml configuration file , them calling sfconfig::get()
method.
Comments
Post a Comment