Import CSV to Symfony 2

I am trying to import a CSV file into Symfony 2. I created a file form and now want to save it in my database.

Here is my handler file where I want to do .csv processing and save it:

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

When I try to test it, the text appears on top of my page:

Array ([fichier] => Symfony \ Component \ HttpFoundation \ File \ UploadedFile Object ([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 [error: Symfony \ Component \ HttpFoundation \ File \ UploadedFile: private] => 0 [pathName: SplFileInfo: private] => / Applications / MAMP / tmp / php / phpSr5O5S [file_name: SplFileInfo: private] => phpSr5O5S))

I do not understand these things.

+6
source share
2 answers

If you want to make the correct symfony2 path, you must create a symfony form to submit the file. For instance:

 // 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(),) ); 

Twig:

 <!-- 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 %} 

Remember that {{ form_enctype(form) }} is important to report that we are sending the file. Symfony2 will generate an enctype="multipart/form-data" tag

+14
source

If you do not need a form, you can do this:

 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. } 
+12
source

Source: https://habr.com/ru/post/925581/


All Articles