douzhi3776 2016-10-04 13:19
浏览 58
已采纳

Zend Form:将图片移动到文件夹中

I'm working on a project with ZF2 and Zend Form. I'd like to add an avatar into a user profile.

The problem is that I only get the file name and save it in the DB. I would like to insert it into a folder so I'll be able to get it and display it. The rest of the form is working.

My guess is that I have to get information from $FILES, but I have no idea how to do this. I've read the documentation but can't see how to apply this to my project.

Thank you in advance!

Here's my Controller Action

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 ;
    }

Here's my form file :

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
         ...         
    }   
}

And the form in my view :

        $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();
  • 写回答

1条回答 默认 最新

  • duanpen9294 2016-10-06 09:04
    关注

    There are two things you could look at for getting your avatar to work:

    1. Using the Gravatar view helper (uses gravatar.com service that automatically links images to email addresses)
      • documentation on using the gravatar service can be found here
    2. Upload images yourself with the file upload classes that are shipped with ZF2:
      • form class for file upload can be found here
      • input filter class documentation can be found here

    If you follow those docs you should be able to manage what you want.


    Note: check especially the use of the Zend\Filter\File\RenameUpload filter in the example in the input filter documentation. This filter renames/moves the uploaded avatar file to the desired location.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'