2016-06-30 16 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ファイルをインポートするために、任意のZendのライブラリを使用する必要はありません

$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); 
} 
関連する問題