zend怎么保存php文件,PHP-如何在Zend中实现文件上传?

本文指导如何在使用 Zend Form 框架创建表单时,正确处理文件上传。作者分享了初始化表单元素的方法,并详细解释了如何在控制器中通过 Zend_File_Transfer_Adapter_Http 适配器接收并存储服务器文件,以解决文件上传后的查找问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我有一个简单的表单,其中包含要上传的电子邮件和文件,它显示正确,提交后,它正确地转到了我设置的结果页面.

但是,我现在找不到服务器上的文件.

这是我的代码:

形成:

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

));

}

我究竟做错了什么?谢谢

解决方法:

在控制器上,您需要一个适配器来接收文件. 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;

}

来源:https://www.icode9.com/content-1-534501.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值