Magento 2 How To Fix Media Directory Trim PHP8.1 Deprecated And Apply m2-hotfixes

Posted underMagento
by Silvia Wang

As of Magento 2.4.4, the system requirement has been changed from PHP7.4 to PHP8.1. However, not all of Magento 2 core code has been fully compatible with PHP8.1. There are various of incompatibility, but currently we want to share one of the instance faced by the community as seen on this post.

The error could be a variety or similar to this message:

trim(): Passing null to parameter #1 ($string) of type string is deprecated in /app/vendor/magento/module-media-storage/App/Media.php on line 151

You can try to extend and replace the problematic line on your custom extension or you can apply this patch:

diff --git a/vendor/magento/module-media-storage/App/Media.php b/vendor/magento/module-media-storage/App/Media.php
index b98dc8ef41e..bc0ac5e56d6 100644
--- a/vendor/magento/module-media-storage/App/Media.php
+++ b/vendor/magento/module-media-storage/App/Media.php
@@ -148,7 +148,7 @@ class Media implements AppInterface
-        $mediaDirectory = trim($mediaDirectory);
+        $mediaDirectory = $mediaDirectory === null ? '' : trim($mediaDirectory);
         if (!empty($mediaDirectory)) {
             // phpcs:ignore Magento2.Functions.DiscouragedFunction
             $this->mediaDirectoryPath = str_replace('\\', '/', $file->getRealPath($mediaDirectory));

Do the followings to apply the patches:

  1. In Magento 2 root folder, if you do not have a folder named “m2-hotfixes”, create one.
  2. Save the above patch source code in a file (e.g. media-trim_2.4.4.patch and put it on folder m2-hotfixes
  3. apply patches by running this script:
<magento_root>/vendor/bin/ece-patches apply

If you cannot run the ece-patches, please take a look at this post on How to Install Magento 2 Quality Patches
From our observation this error happens when someone (or bot) is accessing an non-existing images and triggers server PHP error.
With the same idea as the above, by checking the variable first before trimming it, you can fix the incompatibility issue of Magento 2.4.4 with PHP8.1

More Stories

Cover Image for Magento 2.4.6 Release

Magento 2.4.6 Release

Just a few days ago on March 14, 2023, Adobe released Magento version 2.4.6. Along with this new version, Adobe also sent out patches for older versions: 2.4.5-p2 and 2.4.4-p3. Why is it important to keep you Magento Ecommerce store updated? Generally the main reasons are: Specifically for Magento 2.4.6 these are the changes you […]

Cover Image for How To Optimize Your Magento 2 Store Performance

How To Optimize Your Magento 2 Store Performance

Customer experience is an important factor that influences sales. If the loading time is too long, customers might decide to move on and search for another seller. There are a lot of factors that decides website loading time, such as the hosting, the core code of Magento, third party extensions, and the traffic. Before jumping […]

Cover Image for 5 Tips To Get Your Store Ready For Holiday Shopping

5 Tips To Get Your Store Ready For Holiday Shopping

During peak season, it is advisable to prepare beforehand to make sure your online store is ready to handle increased visits. This might be Christmas, Ramadhan, or maybe company flash sale or events. These are 5 tips you might want to consider before the holiday comes: 1. Predict the increase of traffic Take a look […]

Cover Image for How To Install Magento 2 Quality Patches

How To Install Magento 2 Quality Patches

Magento Quality Patches is a tool that helps developers to check and implement source code patches developed by Adobe and the Magento open source community. Using the code, developers are able to see the list of available patches for your Magento store version, apply it, and / or revert the patches. In order to get […]

How can we help you?

Get in touch with us

Seketik is an Ecommerce development agency that provides various services, including technology development, system audit, optimization, DevOps, hosting, consulting, advisory, support, maintenance, research, security testing & compliance, and related tech services. We are committed to delivering positive impacts to your business through our experiences, expertise, and insight. Technology has moved from luxury to a necessity for businesses to thrive in this ever-challenging environment. We do not build a system for the sake of automation, but also align it to your vision, mission, and business goals by applying an innovative and robust solution to achieve digital transformation.

Copyright © 2023 Seketik. All rights reserved.