Skip to content

Commit

Permalink
[HttpFoundation] Add UploadedFile::getClientOriginalPath() to suppo…
Browse files Browse the repository at this point in the history
…rt directory uploads
  • Loading branch information
danielburger1337 authored and OskarStark committed Dec 4, 2023
1 parent fce123a commit 4120857
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
14 changes: 12 additions & 2 deletions controller/upload_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,23 @@ There are some important things to consider in the code of the above controller:
users. This also applies to the files uploaded by your visitors. The ``UploadedFile``
class provides methods to get the original file extension
(:method:`Symfony\\Component\\HttpFoundation\\File\\UploadedFile::getClientOriginalExtension`),
the original file size (:method:`Symfony\\Component\\HttpFoundation\\File\\UploadedFile::getSize`)
and the original file name (:method:`Symfony\\Component\\HttpFoundation\\File\\UploadedFile::getClientOriginalName`).
the original file size (:method:`Symfony\\Component\\HttpFoundation\\File\\UploadedFile::getSize`),
the original file name (:method:`Symfony\\Component\\HttpFoundation\\File\\UploadedFile::getClientOriginalName`)
and the original file path (:method:`Symfony\\Component\\HttpFoundation\\File\\UploadedFile::getClientOriginalPath`).
However, they are considered *not safe* because a malicious user could tamper
that information. That's why it's always better to generate a unique name and
use the :method:`Symfony\\Component\\HttpFoundation\\File\\UploadedFile::guessExtension`
method to let Symfony guess the right extension according to the file MIME type;

.. note::

If a directory was uploaded, ``getClientOriginalPath`` will contain the **webkitRelativePath** as provided by the browser.
Otherwise this value will be identical to ``getClientOriginalName``.

.. versionadded:: 7.1

The ``getClientOriginalPath`` method was introduced in Symfony 7.1.

You can use the following code to link to the PDF brochure of a product:

.. code-block:: html+twig
Expand Down
10 changes: 7 additions & 3 deletions reference/forms/types/file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ You might calculate the filename in one of the following ways::
// use the original file name
$file->move($directory, $file->getClientOriginalName());

// when "webkitdirectory" upload was used
// otherwise the value will be the same as getClientOriginalName
// $file->move($directory, $file->getClientOriginalPath());

// compute a random name and try to guess the extension (more secure)
$extension = $file->guessExtension();
if (!$extension) {
Expand All @@ -63,9 +67,9 @@ You might calculate the filename in one of the following ways::
}
$file->move($directory, rand(1, 99999).'.'.$extension);

Using the original name via ``getClientOriginalName()`` is not safe as it
could have been manipulated by the end-user. Moreover, it can contain
characters that are not allowed in file names. You should sanitize the name
Using the original name via ``getClientOriginalName()`` or ``getClientOriginalPath``
is not safe as it could have been manipulated by the end-user. Moreover, it can contain
characters that are not allowed in file names. You should sanitize the value
before using it directly.

Read :doc:`/controller/upload_file` for an example of how to manage a file
Expand Down

0 comments on commit 4120857

Please sign in to comment.