Skip to content

Commit 6185887

Browse files
MacDadajaviereguiluz
authored andcommitted
uploading example: explaining the need for md5
1 parent 48cd7f8 commit 6185887

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

controller/upload_file.rst

+13-2
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ Finally, you need to update the code of the controller that handles the form::
136136
/** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */
137137
$file = $product->getBrochure();
138138

139-
// Generate a unique name for the file before saving it
140-
$fileName = md5(uniqid()).'.'.$file->guessExtension();
139+
$fileName = $this->generateUniqueFileName().'.'.$file->guessExtension();
141140

142141
// Move the file to the directory where brochures are stored
143142
$file->move(
@@ -158,6 +157,18 @@ Finally, you need to update the code of the controller that handles the form::
158157
'form' => $form->createView(),
159158
));
160159
}
160+
161+
/**
162+
* @return string
163+
*/
164+
private function generateUniqueFileName()
165+
{
166+
// uniqid() is based on timestamp,
167+
// so it creates similar and predictible file names as a result.
168+
// md5() is used to guarantee equal distribuition
169+
// and smaller predictibility of the file names.
170+
return md5(uniqid());
171+
}
161172
}
162173

163174
Now, create the ``brochures_directory`` parameter that was used in the

0 commit comments

Comments
 (0)