开发者

Need help with Zend_Form_Element_File

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'];
                                           开发者_JAVA百科 $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;


         }




    }


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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜