2016-06-30 3 views
0

zend에 새로 온 것입니다. CSV 파일을 업로드하고 그 내용을 가져 오려고합니다. 내 폼 클래스 내 컨트롤러Zend1에 업로드 된 파일 정보를 얻는 방법

class IndexController extends Zend_Controller_Action { 

    public function indexAction() { 
     $this->view->headTitle('WestWing'); 

     $request = $this->getRequest(); 
     $form = new Application_Form_Upload(); 

     if ($this->getRequest()->isPost()) { 
      if ($form->isValid($request->getPost())) { 
       $formData = new Application_Form_Upload($form->getValues()); 
       echo "<pre>"; 
       print_r($formData); 
       $fileName = $formData->file->tmp_name; 
       echo $fileName; 

      } 
     } else { 
      echo "anot well"; 
     } 

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

} 

<?php 

class Application_Form_Upload extends Zend_Form 
{ 
    public function init() 
    { 
     // Set the method for the display form to POST 
     $this->setMethod('post')->setEnctype('multipart/form-data'); 

     // Add an email element 
     $this->addElement('text', 'email', array(
      'label'  => 'Your email address:', 
      'required' => true, 
      'filters' => array('StringTrim'), 
      'validators' => array(
       'EmailAddress', 
      ) 
     )); 

     // Add the comment element 
     $this->addElement('file', 'csvfile', array(
      'label'  => 'CSV File:', 
      'required' => true 
     )); 


     // Add the submit button 
     $this->addElement('submit', 'submit', array(
      'ignore' => true, 
      'label' => 'Send Request', 
     )); 

     // And finally add some CSRF protection 
     $this->addElement('hash', 'csrf', array(
      'ignore' => true, 
     )); 
    } 
} 

하지만 난이 echo $filename을 수행 할 때, 그것은 나에게 아무 것도 반환하지 않습니다.

답변

0

다른 양식 ($ formData)을 만들 필요가 없습니다. 이미 $ form이 있습니다.

0

방금 ​​fgetcsv 살펴보고, 네이티브 PHP 함수를 사용할 수 있습니다, CSV 파일을 가져올 수있는 젠드 라이브러리를 사용할 필요가 없습니다

$row = 1; 
if (($handle = fopen("test.csv", "r")) !== FALSE) { 
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
     $num = count($data); 
     echo "<p> $num fields in line $row: <br /></p>\n"; 
     $row++; 
     for ($c=0; $c < $num; $c++) { 
      echo $data[$c] . "<br />\n"; 
     } 
    } 
    fclose($handle); 
} 
관련 문제