在Symfony 2中导入CSV

Fre*_*yer 10 csv import symfony

我试图在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;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试测试时,我的页面顶部会显示一些文字:

数组([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 [pathName:SplFileInfo:private] =>/Applications/MAMP/tmp/php/phpSr5O5S [fileName:SplFileInfo:private] => phpSr5O5S))

我不懂那些东西.

Tro*_*ike 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(),)
);
Run Code Online (Sandbox Code Playgroud)

枝条:

<!-- 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 %}
Run Code Online (Sandbox Code Playgroud)

别忘{{ form_enctype(form) }}了告诉我们发送文件很重要.Symfony2将生成enctype="multipart/form-data"标记

  • 在SF3中它应该是` - > add('submitFile',FileType :: class,array('label'=>'File to Submit'))`前面有use语句`use Symfony\Component\Form\Extension\Core \型号\文件类型;` (3认同)

Lou*_*uis 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.
}
Run Code Online (Sandbox Code Playgroud)