分享

PHP-如何在Zend中实现文件上传?

 印度阿三17 2019-10-30

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

这是我的代码:

形成:

 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./content-1-534501.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约