我有一个简单的表单,其中包含要上传的电子邮件和文件,它正确显示,并且在提交后,它会正确地进入我设置的结果页面。 但是,我现在无法在服务器上找到该文件。
这是我的代码:
形式:
public function init()
{
/* Form Elements & Other Definitions Here ... */
$this->setName('form-counting');
$email = new Zend_Form_Element_Text('email');
$email -> setLabel('Name:')
->addFilter('StripTags')
->addValidator('NotEmpty', true)
->addValidator('EmailAddress')
->setRequired(true);
$this->addElement($email);
$UP_file = new Zend_Form_Element_File('UP_file');
$UP_file->setLabel('Upload files:')
->setRequired(true)
$this->addElement($UP_file);
$this->addElement('submit', 'submit', array(
'label' => 'GO',
'ignore' => true
));
}
我在做错了什么?感谢
答案 0 :(得分:3)
哟应该包括,
$file->setDestination(BASE_PATH . '/public/files') // the path you want to set
在您的表单中,
$file = new Zend_Form_Element_File('file');
$file->setLabel('Upload CSV file:')
->setDestination(BASE_PATH . '/public/files')
->setRequired(true)
->addValidator('NotEmpty')
->addValidator('Count', false, 1);
$this->addElement($file);
如上所述..
在Controller中可以执行以下操作,
if ($this->_request->isPost()) {
$formData = $this->_request->getPost();
if ($form->isValid($formData)) {
$uploadedData = $form->getValues();
Zend_Debug::dump($uploadedData, '$uploadedData');
Zend_Debug::dump($fullFilePath, '$fullFilePath'); //[for file path]
}
}
你应该在bootstrap.php中定义以下内容
if(!defined('BASE_PATH')) {
define('BASE_PATH', dirname(__FILE__) . '/..');
}
使用此示例以获得更多理解, ZEND FILE UPLOAD
将您的表单更改为此
class forms_UploadForm extends Zend_Form
{
public function __construct($options = null)
{
parent::__construct($options);
$this->setName('upload');
$this->setAttrib('enctype', 'multipart/form-data');
$description = new Zend_Form_Element_Text('email');
$description->setLabel('Email')
->setRequired(true)
->addValidator('NotEmpty');
$file = new Zend_Form_Element_File('file');
$file->setLabel('File')
->setDestination(BASE_PATH . '/files')
->setRequired(true);
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Upload');
$this->addElements(array($description, $file, $submit));
}
}
答案 1 :(得分:1)
在控制器上,您需要一个适配器来接收文件。 Zend_File_Transfer_Adapter_Http
是一个简单的解决方案。您应该在接收文件的适配器中设置接收目标。
控制器:
public function indexAction()
{
// action body
$eform = new Application_Form_Eform();
if ($this->_request->isPost())
{
$formData = $this->_request->getPost();
if ($eform->isValid($formData))
{
$nameInput = $eform->getValue('email');
//receiving the file:
$adapter = new Zend_File_Transfer_Adapter_Http();
$files = $adapter->getFileInfo();
// You should know the base path on server where you need to store the file.
// You can put the server local address in Zend Registry in bootstrap,
// then have it here like: Zend_Registry::get('configs')->...->basepath or something like that.
// I just assume you will fix it later:
$basePath = "/your_local_address/a_folder_for_unsafe_files/";
foreach ($files as $file => $info) {
// set the destination on server:
$adapter->setDestination($basePath, $file);
// sometimes it would be a good idea to rename the file.
// I left it for you to do it here:
$fileName = $info['name'];
$adapter->addFilter('Rename', array('target' => $fileName), $file);
$adapter->receive($file);
// You can make sure of having the file:
if (file_exists($filePath . $fileName)) {
// you can move, copy or change the file before redirecting to the new page.
// Or you might need to keep the track of files and email in a database.
} else {
// throw error if you want.
}
}
}
// Then redirect:
$this->_helper->redirector->gotoRouteAndExit (array(
'controller' => 'index',
'action' =>'result',
'email' => $nameInput));
}
$this->view->form = $eform;
}