M
M
MasterCopipaster2021-10-11 11:26:24
symfony
MasterCopipaster, 2021-10-11 11:26:24

Symfony5 file upload controller is running out of RAM why?

I ran into a problem, the script crashes with the error
Error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 534773792 bytes) at /vendor/symfony/http-foundation/Request.php:1559

Actually, the meaning of the error is clear. But damn, why? Small files it loads normally, large ones it doesn't have enough memory, but what is it like trying to read the file into the RAM? he should just move it from the temporary folder to the permanent one. It should not try to read the file.

Request.php:1559

if (null === $this->content || false === $this->content) {
            $this->content = file_get_contents('php://input');
        }


What am I doing wrong? here is my controller...
/**
     * @Route("/send/file", name="send_file", methods={"POST"})
     * @param Request $request
     * @param MailerInterface $mailer
     * @param LoggerInterface $logger
     * @return JsonResponse
     * @throws \Exception
     */
    public function newFile(Request $request,MailerInterface $mailer,LoggerInterface $logger)
    {
        $storage = $this->getParameter('file_storage');
        $host = $request->getHost();
        $response = new JsonResponse();
        $form = $this->createForm(FilesType::class);
        $entity_manager = $this->getDoctrine()->getManager();
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $data_form = $form->getData();
            /** @var UploadedFile $brochureFile */
            $form_data = $form->getData();
            $file = $form_data['file'];
            $original_filename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME).'.'.$file->guessExtension();
            $server_filename = Uuid::uuid4().'.'.$file->guessExtension();
            try {
                $file->move(
                    $storage,
                    $server_filename
                );

            } catch (\Exception $e) {
                $logger->error($e);
                return $response->setData([
                    'error' => 'Error uploading file to server',
                    'status' => 'failure'
                ]);
            }
            $files = new Files($form,$server_filename,$original_filename);
            $entity_manager->persist($files);
            $entity_manager->flush();
            $short_key = self::CONTROL_BIT_FILE.KeyGenerator::makeShortKey($files->getId());
            $link_make = $this->collectLinksFiles($short_key,$host,$original_filename);
            $response->setData([
                'status' => 'success',
                'short_key' => $short_key,
                'link' => $link_make->link,
                'html_link' => $link_make->html,
                'bb_link' => $link_make->bb,
                'markdown' => $link_make->markdown,
                'qr_code' => $link_make->qr
            ]);
            if(!is_null($data_form['email'])){
                try {
                    $this->sendEmail($data_form['email'], $link_make->link, $mailer);
                }catch (\Exception $e){
                    $logger->error($e);
                }
            }
        }else{
            $response->setData([
                'error' => 'the form is not filled out correctly',
                'status' => 'failure'
            ]);
        }
        return $response;
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question