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;
}

当我尝试对其进行测试时,我的页面顶部会出现一些文本:

数组 ( [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 [大小:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 491 [错误:Symfony\Component\ HttpFoundation\File\UploadedFile:private] => 0 [pathName: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 回答