需要 Zend_Form_Element_File 的帮助

发布于 2024-11-30 05:35:36 字数 3337 浏览 1 评论 0 原文

我目前正在使用 zend 框架。

我正在制作一个表单来创建一个新的提供商,他们可以将图像上传到其中。

所以我的代码是在提供程序表中创建一个新的提供程序行,然后检索提供程序 ID 以便为提供程序创建新的图像文件夹。

例如,如果provider id = 10,代码将创建一个public/images/providers/10 文件夹

,然后将图像插入到新的图像目录中。

奇怪的是,如果我在接收图像之前删除 $data = $form->getValues();,它会起作用,但是每当 $data = $form->getValues() 在接收图像之前图像它不起作用。

所以我的问题是如何解决这个问题,因为我需要表单值来插入新的提供程序行以便将图像存储在提供程序目录中。

有人可以帮我吗?

预先非常感谢。

这是我的代码(请注意我的表单中名为“logo”的 zend_form_element_file)

public function processNewProviderAction()
    {
          $this->_helper->layout->setLayout('admin');

         $form = $this->getNewProviderForm();

            if ($form->isValid($_POST))
                {

                    $data = $form->getValues();

                    // Insert into provider table
                    $createdBy = 1; // need to get the system User Id
                    $providers = new Application_Model_DbTable_Providers();
                    $providerId = $providers->insertProvider($data, $createdBy);


                 if($form->logo->isUploaded())
                        {

                                    if(isset($providerId))
                                    {
                                            //set the directory for file upload
                                            $directory =            APPLICATION_PATH . '/../public/images/providers/' . $providerId;
                                            if(is_dir($directory))
                                            {//directory exist
                                            }
                                            else
                                            {//create directory
                                                    mkdir($directory, 0777); //0777 is the default which gives the widest access
                                            }

                                            $adapter = $form->logo->getTransferAdapter();
                                            $fileName = $adapter->getFileName('logo');

                                             //getting the extension
                                            $info = pathinfo($fileName);
                                            $baseName = $info['filename'];
                                            $ext = $info['extension'];

                                            $fileName =  $baseName . '.' . $ext;

                                            $attachmentUploadElement = $form->getElement('logo');
                                            $attachmentUploadElement->addFilter('Rename', $directory . '/' . $fileName);
                                            try 
                                                {

                                                    // upload the file
                                                    $form->logo->receive();

                                                }
                                            catch (Zend_File_Transfer_Exception $e)     {        $e->getMessage();    }





                                    }


            }





                }
     else
         { //Form is invalid

          $this->view->form = $form;


         }




    }

Im currently working with zend framework.

Im working on a form to create a new provider and they can upload an image to it.

So my code is to create a new provider row in the provider table and then retrieve the provider id for creating a new image folder for the provider.

Eg if provider id = 10 the code will create a folder of public/images/providers/10

And then insert the image into the new image directory.

The werid thing is that if I remove $data = $form->getValues();before receiving the image, it works but whenever $data = $form->getValues() before receiving the image it doesn't work.

So my question is how can I solve this issue since i needed the form values to insert a new provider row in order to store the image in the provider directory.

Can someone please help me out?

Thanks so much in advance.

Here is my code (please note my zend_form_element_file named "logo" in my form)

public function processNewProviderAction()
    {
          $this->_helper->layout->setLayout('admin');

         $form = $this->getNewProviderForm();

            if ($form->isValid($_POST))
                {

                    $data = $form->getValues();

                    // Insert into provider table
                    $createdBy = 1; // need to get the system User Id
                    $providers = new Application_Model_DbTable_Providers();
                    $providerId = $providers->insertProvider($data, $createdBy);


                 if($form->logo->isUploaded())
                        {

                                    if(isset($providerId))
                                    {
                                            //set the directory for file upload
                                            $directory =            APPLICATION_PATH . '/../public/images/providers/' . $providerId;
                                            if(is_dir($directory))
                                            {//directory exist
                                            }
                                            else
                                            {//create directory
                                                    mkdir($directory, 0777); //0777 is the default which gives the widest access
                                            }

                                            $adapter = $form->logo->getTransferAdapter();
                                            $fileName = $adapter->getFileName('logo');

                                             //getting the extension
                                            $info = pathinfo($fileName);
                                            $baseName = $info['filename'];
                                            $ext = $info['extension'];

                                            $fileName =  $baseName . '.' . $ext;

                                            $attachmentUploadElement = $form->getElement('logo');
                                            $attachmentUploadElement->addFilter('Rename', $directory . '/' . $fileName);
                                            try 
                                                {

                                                    // upload the file
                                                    $form->logo->receive();

                                                }
                                            catch (Zend_File_Transfer_Exception $e)     {        $e->getMessage();    }





                                    }


            }





                }
     else
         { //Form is invalid

          $this->view->form = $form;


         }




    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放手` 2024-12-07 05:35:36

我想我锻炼了。

别担心:)

必须添加这一行

->setValueDisabled(true);

以获取更多信息,请访问

http://www.thomasweidner.com/flatpress/2009/04/17/receering-files-with-zend_form_element_file/

I think i worked out.

Don't worry about it :)

Have to add this line to it

->setValueDisabled(true);

For more info please visit

http://www.thomasweidner.com/flatpress/2009/04/17/recieving-files-with-zend_form_element_file/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文