开发者

How to let the user choose the upload directory?

开发者 https://www.devze.com 2023-02-09 23:51 出处:网络
I have a form used to upload images in my blog en开发者_如何学编程gine. The files are uploaded to web/uploads, but I\'d like to add a \"choice\" widget to let the users pick from a list of folders, fo

I have a form used to upload images in my blog en开发者_如何学编程gine. The files are uploaded to web/uploads, but I'd like to add a "choice" widget to let the users pick from a list of folders, for instance 'photos', 'cliparts', 'logos'.

Here's my 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 is my 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 a 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 do I tell the file upload widget to save the image in a different folder ?


You can concatenate the directory names you said ('photos', 'cliparts', 'logos') to the sf_upload_dir as the code below shows, you will need to create those directories of course.

$this->validatorSchema['file'] = new sfValidatorFile(
          array('path' => sfConfig::get('sf_upload_dir' . '/' . $path)
       ));

Also, you can have those directories detailes in the app.yml configuration file and get them calling to sfConfig::get() method.


I got it to work with the following code :

  public function executeAdd(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())
      {
           //quel est le dossier ?
           switch($this->form->getValue('folder'))
           {
             case 0:
               $this->folder = '/images/clipart/';
               break;
             case 1:
               $this->folder = '/images/test/';
               break;
             case 2:
               $this->folder = '/images/program/';
               break;
             case 3:
               $this->folder = '/images/smilies/';
               break;

           }

           $filename = $this->form->getValue('file')->getOriginalName();
           $this->form->getValue('file')->save(sfConfig::get('sf_web_dir').$this->folder.$filename);

           //path :
           $this->image = $this->folder.$filename;
      }

    }
0

精彩评论

暂无评论...
验证码 换一张
取 消