11

Symfony 2 で .csv ファイルをインポートしようとしています。ファイル フォームを作成しましたが、それをデータベースに保存したいと考えています。

.csv 処理を行い、それを永続化するハンドラー ファイルは次のとおりです。

public function process()
{
    if ($this->request->getMethod() == 'POST')
    {
        $this->form->bindRequest($this->request);

        $tableau = array();
        $i = 0;
        $c = 0;
        $num = 0;

        if (isset($_FILES['file']))
        {
            $file = $_FILES['file']['tmp_name']; 
            $handle = fopen($file,'r'); 
            $row = 1; 
            $handle = fopen("$file", "r"); 

            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
            {
                $num =+ count($data);
                $row++; 

                for ($c = $i; $c < $num; $c++)
                {
                    $tableau[$c] = $data[$c];
                    $i++;
                }
            }
        }
        $tableau[$c+1] = $i;

        /*
        if ($this->form->isValid())
        {
            print_r($this->form->getData());

            $this->onSuccess($this->form->getData());

            return true;
        }
        */
    }

    return false;
}

テストしようとすると、ページの上部にテキストが表示されます。

Array ( [fichier] => Symfony\Component\HttpFoundation\File\UploadedFile オブジェクト ( [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => testcsv.csv [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => text/csv [size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 491 [エラー:Symfony\Component\ HttpFoundation\File\UploadedFile:private] => 0 [パス名:SplFileInfo:private] => /Applications/MAMP/tmp/php/phpSr5O5S [fileName:SplFileInfo:private] => phpSr5O5S ) )

私はそれらのことを理解していません。

4

2 に答える 2

16

適切なsymfony2の方法を実行したい場合は、ファイルを送信するためのsymfonyフォームを作成する必要があります。例えば:

// Your Controller.php
$form = $this->createFormBuilder()
        ->add('submitFile', 'file', array('label' => 'File to Submit'))
        ->getForm();

// Check if we are posting stuff
if ($request->getMethod('post') == 'POST') {
    // Bind request to the form
    $form->bindRequest($request);

    // If form is valid
    if ($form->isValid()) {
         // Get file
         $file = $form->get('submitFile');

         // Your csv file here when you hit submit button
         $file->getData();
    }

 }

return $this->render('YourBundle:YourControllerName:index.html.twig',
    array('form' => $form->createView(),)
);

小枝:

<!-- index.html.twig Twig part -->
{% extends "YourBundle::layout.html.twig" %}

{% block content %}
    <form action="" method="post" {{ form_enctype(form) }}>
        {{ form_widget(form) }}

        <input type="submit" />
    </form>
{% endblock %}

{{ form_enctype(form) }}ファイルを送信していることを伝えることが重要であることを忘れないでください。Symfony2はenctype="multipart/form-data"タグを生成します

于 2012-09-17T15:29:54.607 に答える
15

フォームが必要ない場合は、次のようにします。

public function processAction() {
    foreach($this->getRequest()->files as $file) { 
        if (($handle = fopen($file->getRealPath(), "r")) !== FALSE) {
            while(($row = fgetcsv($handle)) !== FALSE) {
                var_dump($row); // process the row.
            }
        }
    }
    //return response.
}
于 2013-11-20T04:42:16.173 に答える