// filename:module / Test / src / Test / Form / ProfileForm.php
namespace Test\Form;
use Zend\Form\Form;
class ProfileForm extends Form
{
public function __construct($name = null)
{
parent::__construct('Profile');
$this->setAttribute('method', 'post');
$this->setAttribute('enctype','multipart/form-data');
$this->add(array(
'name' => 'profilename',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Profile Name',
),
));
$this->add(array(
'name' => 'fileupload',
'attributes' => array(
'type' => 'file',
),
'options' => array(
'label' => 'File Upload',
),
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Upload Now'
),
));
}
}
// filename:module / Test / view / test / profile / add.phtml
$form = $this->form;
$form->setAttribute('action',
$this->url('Test/profile', //your route name ...
array('controller'=>'profile', 'action' => 'add')));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formRow($form->get('profilename'));
echo $this->formRow($form->get('fileupload'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
就是这样,将创建表单 . 我们如何过滤输入?我们可以通过以下方式创建输入过滤
// filename:module / Test / src / Test / Model / Profile.php
namespace Test\Model;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
class Profile implements InputFilterAwareInterface
{
public $profilename;
public $fileupload;
protected $inputFilter;
public function exchangeArray($data)
{
$this->profilename = (isset($data['profilename'])) ? $data['profilename'] : null;
$this->fileupload = (isset($data['fileupload'])) ? $data['fileupload'] : null;
}
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add(
$factory->createInput(array(
'name' => 'profilename',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
))
);
$inputFilter->add(
$factory->createInput(array(
'name' => 'fileupload',
'required' => true,
))
);
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
如果表单有效,我们可以将文件保存到\ Zend \ File \ Transfer \ Adapter \ Http组件所需的目录,并添加其他验证,例如,添加大小验证以验证最小大小将被接受上传 .
//import Size validator...
use use Zend\Validator\File\Size;
.........
if ($form->isValid()) {
$size = new Size(array('min'=>2000000)); //minimum bytes filesize
$adapter = new \Zend\File\Transfer\Adapter\Http();
//validator can be more than one...
$adapter->setValidators(array($size), $File['name']);
if (!$adapter->isValid()){
$dataError = $adapter->getMessages();
$error = array();
foreach($dataError as $key=>$row)
{
$error[] = $row;
} //set formElementErrors
$form->setMessages(array('fileupload'=>$error ));
} else {
$adapter->setDestination(dirname(__DIR__).'/assets');
if ($adapter->receive($File['name'])) {
$profile->exchangeArray($form->getData());
echo 'Profile Name '.$profile->profilename.' upload '.$profile->fileupload;
}
}
}
// filename:module / Test / src / Test / Controller / ProfileController.php
namespace Test\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Test\Model\Profile,
Test\Form\ProfileForm;
use Zend\Validator\File\Size;
class ProfileController extends AbstractActionController
{
public function addAction()
{
$form = new ProfileForm();
$request = $this->getRequest();
if ($request->isPost()) {
$profile = new Profile();
$form->setInputFilter($profile->getInputFilter());
$nonFile = $request->getPost()->toArray();
$File = $this->params()->fromFiles('fileupload');
$data = array_merge(
$nonFile,
array('fileupload'=> $File['name'])
);
//set data post and file ...
$form->setData($data);
if ($form->isValid()) {
$size = new Size(array('min'=>2000000)); //minimum bytes filesize
$adapter = new \Zend\File\Transfer\Adapter\Http();
$adapter->setValidators(array($size), $File['name']);
if (!$adapter->isValid()){
$dataError = $adapter->getMessages();
$error = array();
foreach($dataError as $key=>$row)
{
$error[] = $row;
}
$form->setMessages(array('fileupload'=>$error ));
} else {
$adapter->setDestination(dirname(__DIR__).'/assets');
if ($adapter->receive($File['name'])) {
$profile->exchangeArray($form->getData());
echo 'Profile Name '.$profile->profilename.' upload '.$profile->fileupload;
}
}
}
}
return array('form' => $form);
}
}
我已经完成了将文件上传到数据库,现在我想从数据库中删除该文件
我已经制作了一个控制器动作,模型和视图上传,但现在我想从数据库中删除该文件
请制作控制器动作和模型类以及查看文件以从数据库中删除文件如何从数据库中删除文件