Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8540101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:31:21+00:00 2026-06-11T11:31:21+00:00

I am trying to import a .csv file in Symfony 2. I’ve created a

  • 0

I am trying to import a .csv file in Symfony 2. I’ve created a file form and now I want to persist it in my database.

Here is my handler file where I want to make the .csv treatment and persist 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’m trying to test it some 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 [fileName:SplFileInfo:private] => phpSr5O5S ) )

I don’t understand those things.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:31:22+00:00Added an answer on June 11, 2026 at 11:31 am

    If you want to do a proper symfony2 way you should create a symfony form to submit the file. For example:

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

    Dont forget that {{ form_enctype(form) }} is important to tell that we are sending file. Symfony2 will generate enctype="multipart/form-data" tag

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to import a trivial CSV to Postgres 8.4 database: Here is
I am trying to import CSV file to SQL server database, with no success.
I am trying to import a txt/csv file into my postgres database from php
I am trying to import a .csv file into my database. I am able
I am trying to import a .csv file into my database. I am able
I am trying to build a import form for a CSV file into the
Now I am trying to import contact details from a csv file. In csv
I am trying to import a CSV file into a MySQL database table. The
I am trying to import a large CSV file into a MySQL database. I
I am trying to import a csv file to a mssql database with the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.