如何使用zf2创建上传表单?

迪迪努

我正在创建一个用户个人资料表单,我想添加一个图片上传input.its更新表单。这是我的表格。在这种情况下,我试图将图像文件存储在图像文件夹中,我将获取文件名并将其存储在数据库中。

<?php

namespace Users\Form;

use Zend\Form\Form;

class AccountForm extends Form {
public function __construct($name = null) {
    // we want to ignore the name passed
    parent::__construct ( 'registration' );
    $this->setAttribute ( 'method', 'post' );

    $this->add ( array (
            'name' => 'email',
            'attributes' => array (
                    'type' => 'email',
                    'class' => 'form-control',
                    'placeholder' => 'Email Address' 
            ) 
    ) );

    $this->add(array(
            'name' => 'upload',
            'attributes' => array(
                    'type'  => 'file',                  
            ),
    ));

    $this->add ( array (
            'name' => 'fname',
            'attributes' => array (
                    'type' => 'text',
                    'class' => 'form-control',
                    'placeholder' => 'First Name' 
            ) 
    ) );

    $this->add ( array (
            'name' => 'lname',
            'attributes' => array (
                    'type' => 'text',
                    'class' => 'form-control',
                    'placeholder' => 'Last Name' 
            ) 
    ) );

    $this->add ( array (
            'name' => 'phone',
            'attributes' => array (
                    'type' => 'phone',
                    'class' => 'form-control',
                    'placeholder' => 'Phone Number' 
            ) 
    ) );

    $this->add ( array (
            'name' => 'address1',
            'attributes' => array (
                    'type' => 'text',
                    'class' => 'form-control',
                    'placeholder' => 'Address1'
            )
    ) );

    $this->add ( array (
            'name' => 'address2',
            'attributes' => array (
                    'type' => 'text',
                    'class' => 'form-control',
                    'placeholder' => 'Address2'
            )
    ) );

    $this->add ( array (
            'name' => 'company',
            'attributes' => array (
                    'type' => 'text',
                    'class' => 'form-control',
                    'placeholder' => 'Company'
            )
    ) );

    $this->add ( array (
            'name' => 'zipcode',
            'attributes' => array (
                    'type' => 'text',
                    'class' => 'form-control',
                    'placeholder' => 'ZIP Code'
            )
    ) );

    $this->add ( array (
            'name' => 'province',
            'attributes' => array (
                    'type' => 'text',
                    'class' => 'form-control',
                    'placeholder' => 'Province'
            )
    ) );
    $this->add ( array (
            'name' => 'city',
            'attributes' => array (
                    'type' => 'text',
                    'class' => 'form-control',
                    'placeholder' => 'City'
            )
    ) );

    $this->add ( array (
            'name' => 'submit',
            'attributes' => array (
                    'type' => 'submit',
                    'value' => 'Submit',
                    'class' => 'btn bg-olive btn-block' 
            )

    ) );

}

}

这是行动

   public function accountAction() {
    $this->layout ( 'layout/layoutin' );
    $user_session = new Container ( 'user' );
    $username = $user_session->username;

    $form = new AccountForm ();
    $request = $this->getRequest ();

    if ($request->isPost ()) {
        $prof = new Profile ();
        $form->setInputFilter ( $prof->getInputFilter () );
        $form->setData ( $request->getPost () );

        if ($form->isValid ()) {
            $prof->exchangeArray ( $form->getData () );
            $reply = $this->getUsersTable ()->updateUsers ( $prof, $username );
   }
          }
         return array (
               'form' => $form 
        );
       // This shows the :controller and :action parameters in default route
       // are working when you browse to /users/users/foo
     }

和输入过滤器是。

    $inputFilter->add(
                array(
                        'name' => 'upload',
                        'required' => true,
                        'filters' => array(
                                array(
                                          'name' => 'File\RenameUpload',
                                        'options' => array(
                                                'target' => './public/img/',
                                                'randomize' => true,
                                        ),
                                ),
                        ),
                )
        );
米海

您可以在此处找到一些示例:https : //github.com/cgmartin/ZF2FileUploadExamples

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章