首页 / PHP / PHP-如何在Zend中实现文件上传?
PHP-如何在Zend中实现文件上传?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP-如何在Zend中实现文件上传?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2287字,纯文字阅读大概需要4分钟。
内容图文
![PHP-如何在Zend中实现文件上传?](/upload/InfoBanner/zyjiaocheng/678/062470d3433c48cf82668a0b3019dc1a.jpg)
我有一个简单的表单,其中包含要上传的电子邮件和文件,它显示正确,提交后,它正确地转到了我设置的结果页面.
但是,我现在找不到服务器上的文件.
这是我的代码:
形成:
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;
}
内容总结
以上是互联网集市为您收集整理的PHP-如何在Zend中实现文件上传?全部内容,希望文章能够帮你解决PHP-如何在Zend中实现文件上传?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。