Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6185887

Browse files
MacDadajaviereguiluz
authored andcommitted
uploading example: explaining the need for md5
1 parent48cd7f8 commit6185887

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

‎controller/upload_file.rst

Lines changed: 13 additions & 2 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp