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

  • SEARCH
  • Home
  • 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 8098721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:09:51+00:00 2026-06-05T22:09:51+00:00

While uploading a file bigger than post_max_size in Symfony, the uploaded file is allocated

  • 0

While uploading a file bigger than post_max_size in Symfony, the uploaded file is allocated in the memory.

Fatal error: Allowed memory size of 150994944 bytes exhausted (tried
to allocate 84627994 bytes) in
/Applications/MAMP/htdocs/Symfony/vendor/symfony/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php
on line 28

Why is symfony trying to allocate a file to the memory on POST?

1/ php.ini

file_uploads = On
upload_tmp_dir = /Applications/MAMP/tmp/php
upload_max_filesize = 32M
post_max_size = 48M

2/ THE CONTROLLER

<?php
//AcmeDemoBundle/Controller/DemoController
namespace Acme\DemoBundle\Controller;
use Symfony\Component\HttpFoundation\Request;

class DemoController extends Controller
{

public function createAction(Request $request)
{

    if ($request->getMethod() == 'POST')
    {
        if ($_FILES["file"]["size"] < 3000000 )//3Mb
        {
            if ($_FILES["file"]["error"] > 0)
            {
                echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
            }
            else
            {
                if (empty($_POST) && empty($_FILES) && isset($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) == 'post')
                {
                    echo "The file is bigger than post_max_size in php.ini.";
                }
                else
                {
                    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";

                    if (file_exists("upload/" . $_FILES["file"]["name"]))
                    {
                        echo $_FILES["file"]["name"] . " already exists. ";
                    }
                    else
                    {
                        move_uploaded_file($_FILES["file"]["tmp_name"],
                            "upload/" . $_FILES["file"]["name"]);
                        echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
                    }
                }
            }
        }
        else
        {
            echo "Invalid file";
        }

    }

    return $this->container->get('templating')->renderResponse('AcmeDemoBundle:Demo:create.html.twig');

}

3/ THE TEMPLATE

//AcmeDemoBundle/Resources/views/Demo/create.html.twig
{% extends "AcmeDemoBundle::layout.html.twig" %}

{% block content %}

<h1>Upload File</h1>

<form action="#" method="post"
      enctype="multipart/form-data">
    <label name="file">Filename:</label>
    <input type="file" name="file" id="file" />
    <br />
    <input type="submit" name="submit" value="Submit" />
</form>

{% endblock %}
  • 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-05T22:09:52+00:00Added an answer on June 5, 2026 at 10:09 pm

    I assume that you’re trying symfony via the app_dev.php front controller.

    By default, in the dev environment, the profiler is enabled for every requests.

    framework:
        profiler: { only_exceptions: false }
    

    Just change the only_exceptions param from false to true, and try again.

    framework:
    profiler: { only_exceptions: true }

    You should use the tools that the framework provides you to handle file upload.

    Action:

    public function createAction(Request $request)
    {
        $form = $this->createFormBuilder()
            ->add('attachment', 'file')
            ->getForm();
    
        if ($request->getMethod() == 'POST') {
            $form->bindRequest($request);
    
            if ($form->isValid()) {
                $file = $form['attachment']->getData(); /** @var $file \Symfony\Component\HttpFoundation\File\UploadedFile */
    
                echo $file->getClientSize();
            }
        }
    
        return $this->render('AcmeDemoBundle:Demo:create.html.twig', array(
            'form' => $form->createView(),
        ));
    }
    

    Template:

    {% extends "AcmeDemoBundle::layout.html.twig" %}
    
    {% block content %}
    
    <h1>Upload File</h1>
    
    <form action="" method="post" {{ form_enctype(form) }}>
        {{ form_errors(form) }}
    
        {{ form_row(form.attachment) }}
    
        {{ form_rest(form) }}
    
        <input type="submit" />
    </form>
    
    {% endblock %}
    

    With this code, php never exceeds memory usage. If the file is too big, the form show an error, and the code inside “if ($form->isValid())” is not executed.

    Have a look at the form documentation : http://symfony.com/doc/current/book/forms.html
    and the reference for the “file” field type : http://symfony.com/doc/current/reference/forms/types/file.html

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

Sidebar

Related Questions

I am getting the following error while uploading the file from my local drive.
While user uploading a file, is it possible to know if the uploaded file
I'm Getting this error in my development log while uploadify is uploading the file
is it necessary to mention content-type in http header while uploading the file. i
I'm getting IndexOutOfBoundsException while uploading a zip file. What could be the reason? How
How to validate corrupt image file while uploading using Rails if the corrupted jpg
I have some weird problem while uploading a file with a Cyrillic name using
I'm currently having an issue while uploading a file from Html to my rest
I want to add watermark on photos while uploading with paperclip I've got same
I am getting this errorBundle Identifier differs from prior bundle identifier while uploading 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.