Fixed Server error: Attachment uploader not handling utf-8 filenames correctly

S Thomas

Well-known member
Affected version
2.0.7
Create a new text file, add any content and take this as a filename (which is totally valid under Windows at least):
Ağyar için ey bi-vefa (Eski yazı) - Numan Ağa # Tanburi, Musahib#i Şehryari - Muhayyer Sünbüle - v1.txt

This will result in a server error:
Code:
Oops! We ran into some problems.
InvalidArgumentException: Received invalid UTF-8 for string column [filename] in src\XF\Mvc\Entity\Entity.php at line 688

    XF\Mvc\Entity\Entity->_castValueToType() in src\XF\Mvc\Entity\Entity.php at line 575
    XF\Mvc\Entity\Entity->set() in src\XF\Service\Attachment\Preparer.php at line 34
    XF\Service\Attachment\Preparer->insertDataFromFile() in src\XF\Service\Attachment\Preparer.php at line 16
    XF\Service\Attachment\Preparer->insertAttachment() in src\XF\Attachment\Manipulator.php at line 170
    XF\Attachment\Manipulator->insertAttachmentFromUpload() in src\XF\Pub\Controller\Attachment.php at line 86
    XF\Pub\Controller\Attachment->actionUpload() in src\XF\Mvc\Dispatcher.php at line 249
    XF\Mvc\Dispatcher->dispatchClass() in src\XF\Mvc\Dispatcher.php at line 88
    XF\Mvc\Dispatcher->dispatchLoop() in src\XF\Mvc\Dispatcher.php at line 41
    XF\Mvc\Dispatcher->run() in src\XF\App.php at line 1931
    XF\App->run() in src\XF.php at line 328
    XF::runApp() in index.php at line 13
Reproducible here on XF.
 
Last edited:
Should fix it.
Code:
 src/XF/Entity/AttachmentData.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/XF/Entity/AttachmentData.php b/src/XF/Entity/AttachmentData.php
index ee763fe..d71426d 100644
--- a/src/XF/Entity/AttachmentData.php
+++ b/src/XF/Entity/AttachmentData.php
@@ -179,7 +179,7 @@ class AttachmentData extends Entity
                 $extension = '';
             }
 
-            $fileName = substr($info['filename'], 0, $maxLength - strlen($extension)) . $extension;
+            $fileName = mb_substr($info['filename'], 0, $maxLength - strlen($extension)) . $extension;
         }
 
         return true;
 
Back
Top Bottom