如何让用户选择上传目录? [英] How to let the user choose the upload directory?
本文介绍了如何让用户选择上传目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表单用于在我的博客引擎中上传图片。这些文件上传到 web / uploads ,但我想添加一个选择小工具,让用户从文件夹列表中选择,例如照片,剪贴画,标志。
这里是我的表单
class ImageForm extends BaseForm
{
public function configure()
{
$ this-> widgetSchema-> setNameFormat('image [%s]');
$ b $ 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
)
));
$ b $ this-> setWidget('folder',new sfWidgetFormChoice(array(
'expanded'=> false,
'multiple'=> false,
'choices'=> array('photos','cliparts','logos')
)
));
$ this-> setValidator('folder',new sfValidatorChoice(array(
'choicess => array(0,1,2)
)));
code $
这是我的动作:
$ b
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') - >保存();
$ this-> image = $ this-> form-> getValue('file');
$ b
一个自定义的文件验证器:
class mySfValidatorFile extends sfValidatorFile
{
($ options = array(),$ messages =
array())
{
parent :: configure();
$ this-> addOption('validated_file_class',
'sfValidatedFileFab');
$ b类sfValidatedFileFab扩展sfValidatedFile
public function generateFilename()
return $这 - > getOriginalName();
$ b 那么如何告诉文件上传小部件保存图片在不同的文件夹?
解决方案我得到它与以下代码工作:
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;
案例1:
$ this-> folder ='/ images / test /';
break;
情况2:
$ this-> folder ='/ images / program /';
break;
情况3:
$ this-> folder ='/ images / smilies /';
break;
$ b $ filename = $ this-> form-> getValue('file') - > getOriginalName();
$ this-> form-> getValue('file') - > save(sfConfig :: get('sf_web_dir')。$ this->文件夹。
//路径:
$ this-> image = $ this->文件夹。
}
}
I have a form used to upload images in my blog engine. 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 ?
解决方案 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;
}
}
这篇关于如何让用户选择上传目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文