Updated 21 December 2016
Here we will learn how to make a controller to upload a file and download the file in Symfony. Its quiet easy as in Symfony its quiet similar to how you do it in core PHP. So lets start! We are creating a controller which will upload a file and save its entry in a database table let the Entity that correspond to the Database Table is ‘UploadedFile’. Our entity ‘ UploadedFile’ will have 4 columns:
So lets start with our controller thats DefaultController, we have to create an action lets take uploadAction() so the controller will look like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
/** * @Route("/api/uploadfile") * Method("POST") */ public function uploadAction(Request $request) { try { $file = $request->files->get ( 'my_file' ); $fileName = md5 ( uniqid () ) . '.' . $file->guessExtension (); $original_name = $file->getClientOriginalName (); $file->move ( $this->container->getParameter ( 'file_directory' ), $fileName ); $file_entity = new UploadedFile (); $file_entity->setFileName ( $fileName ); $file_entity->setActualName ( $original_name ); $file_entity->setCreated ( new \DateTime () ); $manager = $this->getDoctrine ()->getManager (); $manager->persist ( $file_entity ); $manager->flush (); $array = array ( 'status' => 1, 'file_id' => $file_entity->getFileId () ); $response = new JsonResponse ( $array, 200 ); return $response; } catch ( Exception $e ) { $array = array('status'=> 0 ); $response = new JsonResponse($array, 400); return $response; } } |
Now we have to create a parameter named ‘file_directory’ in config.yml file to declare the path where the file is to be stored. So go to app/config/config.yml and add the below mentioned line under ‘parameters:‘ tag
1 |
file_directory: '%kernel.root_dir%/../web/uploads/assets' |
Now its done when you will upload a file now the file will be stored in the path with a different name and you can see the entry in database. Now send your file as a form data and you can see this working.
Its time for the next step, as now we have file in our system so we are creating an action to download that file. You can take any way to distinguish between files but here I am using FileId so our link (route) will have FileId in it as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
/** * @Route("/api/downloadfile/{id}") */ public function downloadAction($id) { try { $file = $this->getDoctrine ()->getRepository ( 'AppBundle:UploadedFile' )->find ( $id ); if (! $file) { $array = array ( 'status' => 0, 'message' => 'File does not exist' ); $response = new JsonResponse ( $array, 200 ); return $response; } $displayName = $file->getActualName (); $fileName = $file->getFileName (); $file_with_path = $this->container->getParameter ( 'file_directory' ) . "/" . $fileName; $response = new BinaryFileResponse ( $file_with_path ); $response->headers->set ( 'Content-Type', 'text/plain' ); $response->setContentDisposition ( ResponseHeaderBag::DISPOSITION_ATTACHMENT, $displayName ); return $response; } catch ( Exception $e ) { $array = array ( 'status' => 0, 'message' => 'Download error' ); $response = new JsonResponse ( $array, 400 ); return $response; } } |
And you are ready to go! Access the link and you will have your file downloaded with the name it was uploaded not with the name it was saved on your server. For more info go to Symfony Components.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
it returns null