Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment | File | Size | Author |
---|---|---|---|
#14 | 2932564-14.patch | 716 bytes | jrockowitz |
| |||
#13 | Screen Shot 2017-12-26 at 7.57.43 PM.png | 78.41 KB | 0Sarah0Al |
#11 | file-name-length-2932564.patch | 2.33 KB | 0Sarah0Al |
#8 | Screen Shot 2017-12-23 at 10.52.12 PM.png | 88.99 KB | 0Sarah0Al |
#6 | Screen Shot 2017-12-22 at 6.55.59 PM.png | 85.51 KB | jrockowitz |
Comments
Comment #2
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedThis would be a new feature.
Comment #4
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedComment #6
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedI committed the patch. Please download the latest dev release to review.
Comment #7
0Sarah0Al CreditAttribution: 0Sarah0Al commentedHi 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
Comment #8
0Sarah0Al CreditAttribution: 0Sarah0Al commentedComment #9
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedI was able to fix 1. and get transliterate() to work on 'عربى.txt'. Please download the latest dev release to test.
Comment #11
0Sarah0Al CreditAttribution: 0Sarah0Al commentedHi 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
Comment #12
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedCan you please give me an example of a long file name?
Comment #13
0Sarah0Al CreditAttribution: 0Sarah0Al commentedThis 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.
Comment #14
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedI 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.
Comment #15
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedDrupal 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.
Comment #17
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedI applied the patch that fixed the bug. If you want to limit the file name's length you should use custom code.