2013-12-13 65 views
0

我有一個「addForm」和「editForm」。在addForm中添加文檔文件後,它將保存在我的數據庫中。如果我想編輯這個表單,我必須再次上傳這個文檔。舊文件將被刪除。我想創建一個函數來檢查文檔是否已經上傳,所以我不必每次都要上傳它來編輯一個項目。我只是不知道從哪裏開始。一點幫助將是偉大的。Zend1表格上傳文檔文件

我添加/ editform:

$pdf = new Zend_Form_Element_File('document'); 
    $pdf->setLabel('Nieuwe PDF') 
      ->addValidator('extension', true, array('docx', 
           'docx','pdf','txt')) 
      ->addValidator('Count', false, 1) 
      ->addValidator('Size', false, 10240000) 
      ->setDestination(PUBLIC_PATH . '/../data/invoicespdf/') 
      ->setRequired(false); 

回答

0

嘗試在控制器使用此...修改以滿足您的需求:

if ($request->isPost()) { 
       if ($form->isValid($request->getPost())) { 
        if ('administrator' == $user->role) { 
         $oldFileName = $form->getElement('oldfilename')->getValue(); //the hidden field 
         $data = $form->getValues(); 
         $model->populate($data); 
        if (file_exists('uploads/cv/' . $oldFileName)) { 
          $form->getElement('cv')->setIgnore(true); //this is my Form File Element - the file exists, I don't need to store the filename 
         } else { // if you want you can unlink $oldFileName 
          $upload = new Zend_File_Transfer_Adapter_Http(); 
          $info = $upload->getFileInfo('cv'); 
          $upload->setDestination("uploads/cv/"); 
          if (file_exists('uploads/cv/' . $info['cv']['name'])) { 
           $newFileName = time() . rand(0, 100000) . "-" . $info['cv']['name']; // I need to avoid overwriting file 
          } else { 
           $newFileName = $info['cv']['name']; 
           $upload->addFilter('Rename', $newFileName); 
          } 
          try { 
           $upload->receive(); 
          } catch (Zend_File_Transfer_Exception $e) { 
           $e->getMessage(); 
          } 
         } 
         $model->save(); 
         return $this->_helper->redirector('list'); 
        } else { 
         //some error message 
         $this->_helper->redirector('list'); 
        } 
       } else { //form not valid 
        $this->view->form = $form; 
       } 
      } else { 
       $model->find($id); 
       $data = array(); 
       $data = $model->toArray(); 
       $data['oldfilename'] = $model->get_cv(); //the filename stored in db 
       $form->getElement('cv')->setRequired(false); 
       $form->populate($data); 
       $this->view->form = $form; 
      }