【发布时间】:2016-10-04 13:19:38
【问题描述】:
我正在使用 ZF2 和 Zend Form 开展一个项目。我想将头像添加到用户个人资料中。
问题是我只获取文件名并将其保存在数据库中。我想把它插入一个文件夹,这样我就可以得到它并显示它。表格的其余部分正在工作。
我的猜测是我必须从 $FILES 获取信息,但我不知道该怎么做。我已阅读文档,但看不到如何将其应用于我的项目。
提前谢谢你!
这是我的控制器动作
public function signinAction()
{
$this->em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
$form = new SignupForm($this->em);
$model = new ViewModel(array("form" => $form));
$url = $this->url()->fromRoute("signin");
$prg = $this->prg($url, true);
if($prg instanceof \Zend\Http\PhpEnvironment\Response){
return $prg;
}
else if($prg === false){
return $model;
}
$user = new User();
$form->bind($user) ;
$form->setData($prg) ;
if($form->isValid()){
$bcrypt = new Bcrypt() ;
$pwd = $bcrypt->create($user->getPassword());
$user->setPassword($pwd);
$this->em->persist($user) ;
$this->em->flush() ;
return $this->redirect()->toRoute('login');
}
return $model ;
}
这是我的表单文件:
class SignupForm extends Form
{
private $em = null;
public function __construct($em = null) {
$this->em = $em;
parent::__construct('frm-signup');
$this->setAttribute('method', 'post');
$this->setHydrator(new DoctrineEntity($this->em, 'Application\Entity\User'));
//Other fields
...
//File
$this->add(array(
'type' => "File",
'name' => 'avatar',
'attributes' => array(
'value' => 'Avatar',
),
));
//Submit
...
}
}
我认为的形式:
$form = $this->form;
echo $this->form()->openTag($form);
//other formRow
echo $this->formFile($form->get('avatar'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
【问题讨论】:
标签: php zend-framework zend-framework2 zend-form