Hi there,
is there a way to trim/clean file’s name after upload?

I have a webform where I ask users to upload files (docs and images), However users can upload files with bad-looking file names ,,(alot of weird characters and/or spaces). I want to trim/clean the files' names before they are saved on the file system.

Thanks

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Sarahphp1 created an issue. See original summary.

jrockowitz’s picture

This would be a new feature.

  • jrockowitz committed 2181fdd on 2932564-clean-file-name
    Issue #2932564: trim/clean file's name after file upload
    
jrockowitz’s picture

Status: Active » Needs review
FileSize
4.77 KB

  • jrockowitz committed 2957f18 on 8.x-5.x
    Issue #2932564 by jrockowitz: trim/clean file's name after file upload
    
jrockowitz’s picture

Status: Needs review » Fixed
FileSize
85.51 KB

I committed the patch. Please download the latest dev release to review.

0Sarah0Al’s picture

Hi Jacob

Thanks for your prompt response and the patch.

I think there is a problem. It could be from the transliterate() function.

1-first the file extension is printed two times.
2-if the characters all Arabic letters, the patch completely deletes the file name and again prints the file extension 2 times.

please see image.

Thanks again

0Sarah0Al’s picture

jrockowitz’s picture

I was able to fix 1. and get transliterate() to work on 'عربى.txt'. Please download the latest dev release to test.

  • jrockowitz committed 4feb0fb on 8.x-5.x
    Issue #2932564 by jrockowitz, Sarahphp1: trim/clean file's name after...
0Sarah0Al’s picture

Hi Jacob

I think the last commit fixed it ,, but not for the very long file names ,,

I tried to extend your patch to include another field under 'Sanitize' for file name character length ,, But I could not make it work, unfortunately :(
I did not know what I did wrong in my code ..
it must be the field name ,,

Thanks

jrockowitz’s picture

Can you please give me an example of a long file name?

0Sarah0Al’s picture

This is the file I tried to upload..

نتform_uploads_ اتناانماامنااتانتام ,., 27233 57_4bc3 [] c498-62ef- && اتنىلالا 495a-bd47-acddd9dae270_2-Marios+Photo.JPG

Also, please see the image attached.

jrockowitz’s picture

Status: Fixed » Needs review
FileSize
716 bytes

I am not sure we should be trimming file names.

I did find a bug where the submission id in the path was not being delimited with a forward slash correctly.

jrockowitz’s picture

Drupal core already limits filenames to 240 characters.

https://api.drupal.org/api/drupal/core%21modules%21file%21file.module/fu...

Also see #193954: {file}_uri and {file}_filename length limitations

Finally using custom code you can add a custom $element['#upload_validators'] to any file upload.

  • jrockowitz committed f9f415d on 8.x-5.x
    Issue #2932564 by jrockowitz, Sarahphp1: trim/clean file's name after...
jrockowitz’s picture

Status: Needs review » Fixed

I applied the patch that fixed the bug. If you want to limit the file name's length you should use custom code.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.