Skip to content

[HttpFoundation] Add UploadedFile::getClientOriginalPath() to support directory uploads #19216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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