We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29902
    • 72 Posts
    I have looked all over the forum for a way to rename a file/image on upload. I got a fix for it working in Evolution, but I cannot seem to find any fix for it for Revolution.

    I live in Norway and alot of filenames have norwegian letters in them and the users of our MODX sites (well over 100) are constantly forgetting to rename the files (removing spaces and removing norwegian letters) and that messes up stuff like phpthumb that cannot understand image-names with space in them or with weird norwegian letters.

    I have seen in the forums that questions like this are ignored by the big guys. But I hope there is a fix for this soon or in the future. I have been developed sites in MODX since the beginning, and this has allways been an overlooked subject from the developers.

    Nice if it got some attention and could be nice to work something like the Translit package or something.

    Filenames the users are uploading are like this:

    "peter prøver å finne seg selv.jpg"

    should be on upload

    "peter_prover_a_finne_seg_selv.jpg"

    With over 100 sites produced in MODX this is starting to take a toll on me correcting all the image-names and file-names because of the users not remembering to rename the file before upload.
      ...I believed it was Friday...
    • I would be interested in a solution like this as well; my problem is that I have a file upload where a link to the uploaded file is sent in the email report, but a bug in PHP move_uploaded_file has the charset of the file's name converted to the host's Windows charset, so the URL in the emailed link is bad. What's needed is a way to convert the spaces and special characters to plain ASCII and underscores before the form is actually processed and the uploaded file gets moved as well as being changed in the fields before the email report is generated. Or something.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 31946
        • 116 Posts
        I just had problems with this too, because in Switzerland you also have ö/ü/ä etc.
        A rather quick and dirty fix that worked for me:
        Open the modfilemediasource.class.php in core/model/modx/sources and add the following after line 656 (the one with the comment):
        $newPath = $this->fileHandler->sanitizePath($file['name']);
        $newPath = preg_replace('/[^A-Za-z0-9\.]/','_',$newPath); //This replaces all ohter characters than a-z0-9. with underscore
        $newPath = $directory->getPath().$newPath;

        That will replace your characters with _

        Maybe a system setting to define this regex would be a solution? Be careful when upgrading modx, you will loose this fix...
          • 38339
          • 41 Posts
          Maybe a system setting to define this regex would be a solution? Be careful when upgrading modx, you will loose this fix...
          Think, you should put in a feature request for this.
          Or maybe someone could create a plugin?

          Manfred
            Revolution 2.2.6
            • 3749
            • 24,544 Posts
            Do you have all tables and fields in the DB set to UTF-8 (and MODX as well)?


            ---------------------------------------------------------------------------------------------------------------
            PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
            MODx info for everyone: http://bobsguides.com/modx.html
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
            • That doesn't help; in my case the problem is with PHP and move_uploaded_file on a Windows server.

              move_uploaded_file will use the system character set for the filename, which will not match that of the filename as uploaded. In the case of eForm sending a link to the uploaded file in its report, the report is made from the file upload field in utf-8, but the filename will have been converted to whatever the server's system character set is. In the case of plain ASCII filenames that's not a problem, but get any special characters such as French or German accented characters, or Japanese or other non-ASCII characters and the link in the report won't work.

              This may be the problem with phpthumb; it can't find the image because it is looking for a filename that was stored as it was uploaded, but the move_uploaded_file process trashed the original filename. This may not be obvious when looking at the file, since the browser or the FTP client will show the filename as it is on the server, which with most Western charsets will look OK, while major differences like Cyrillic or Japanese will show odd characters.
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
              • Try this plugin to sanitizing filenames: https://github.com/omycode/SanitizeFilename
                  Freelance Web Developer who likes MODX, PHP, jQuery, ExtJS, SASS and other cool web things. Currenty doing great stuff at COEX CZ.

                  GitHub: https://github.com/hansek
                  Follow me: https://twitter.com/jantezner
                  Or follow us: https://twitter.com/coexcz

                  http://www.modx.cz || http://www.coex.cz
                  • 29902
                  • 72 Posts
                  Quote from: Wanze at Feb 03, 2012, 07:45 PM
                  I just had problems with this too, because in Switzerland you also have ö/ü/ä etc.
                  A rather quick and dirty fix that worked for me:
                  Open the modfilemediasource.class.php in core/model/modx/sources and add the following after line 656 (the one with the comment):
                  $newPath = $this->fileHandler->sanitizePath($file['name']);
                  $newPath = preg_replace('/[^A-Za-z0-9\.]/','_',$newPath); //This replaces all ohter characters than a-z0-9. with underscore
                  $newPath = $directory->getPath().$newPath;

                  That will replace your characters with _

                  Maybe a system setting to define this regex would be a solution? Be careful when upgrading modx, you will loose this fix...

                  This worked and is a fix that is ok for me.

                  I hope that a fix like this would be standard in MODX Revolution soon.
                    ...I believed it was Friday...
                  • Awesome... you don't know how much of a pain this has been with clients....
                    BTW, a few sites we work with which we haven't upgrade to 2.2, was able to make same adjustment in the following file.

                    /core/mode/modx/processor/browser/file/upload.php
                      "Give a company a website, and they will be successful for now...
                      Give a company a long-term web strategy, and they will be successful for a lifetime."

                      Company Website: http://www.orbitalalliance.com
                      Company Blog: http://www.orbitalalliance.com/blog
                    • Thanks! I was looking for a solution for it for a long time!

                      With the plugin suggested by Hansek the solution could be upgrading-safe?

                      Or may be a plugin to be launched as soon as file is uploaded?
                        TilliLab | MODX Ambassador
                        website