From 8ef7c12e450bd3367f4280c7e5ea5d7a8b01f658 Mon Sep 17 00:00:00 2001 From: Dawid Nowak Date: Wed, 31 Jan 2018 01:53:18 +0100 Subject: [PATCH 1/2] uploading example: explaining the need for md5 --- controller/upload_file.rst | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/controller/upload_file.rst b/controller/upload_file.rst index dda7491124d..06486999d90 100644 --- a/controller/upload_file.rst +++ b/controller/upload_file.rst @@ -132,8 +132,7 @@ Finally, you need to update the code of the controller that handles the form:: /** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */ $file = $product->getBrochure(); - // Generate a unique name for the file before saving it - $fileName = md5(uniqid()).'.'.$file->guessExtension(); + $fileName = $this->generateUniqueFileName().'.'.$file->guessExtension(); // Move the file to the directory where brochures are stored $file->move( @@ -154,6 +153,18 @@ Finally, you need to update the code of the controller that handles the form:: 'form' => $form->createView(), )); } + + /** + * @return string + */ + private function generateUniqueFileName() + { + // uniqid() is based on timestamp, + // so it creates similar and predictible file names as a result. + // md5() is used to guarantee equal distribuition + // and smaller predictibility of the file names. + return md5(uniqid()); + } } Now, create the ``brochures_directory`` parameter that was used in the From 5f00f817857e78057f2ae20fff0e1f2af21ec3e1 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Wed, 31 Jan 2018 08:47:24 +0100 Subject: [PATCH 2/2] Reword a code comment --- controller/upload_file.rst | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/controller/upload_file.rst b/controller/upload_file.rst index 06486999d90..2c6e7434b54 100644 --- a/controller/upload_file.rst +++ b/controller/upload_file.rst @@ -159,10 +159,8 @@ Finally, you need to update the code of the controller that handles the form:: */ private function generateUniqueFileName() { - // uniqid() is based on timestamp, - // so it creates similar and predictible file names as a result. - // md5() is used to guarantee equal distribuition - // and smaller predictibility of the file names. + // md5() reduces the similarity of the file names generated by + // uniqid(), which is based on timestamps return md5(uniqid()); } }