This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

murphy.adrian28211
murphy.adrian28211

Hi, I'm new to October and I'm using the rainlab pages plugin. I am using a mediafinder variable type to allow editors to upload background images for certain areas of the site.

One of the first things that an editor did was upload a file with spaces in the file name. So the background image is not showing because of the spaces.

Is there any way to easily remove spaces and otherwise sanitize file names on upload? I have been unable to find a plugin to do this.

Frankly I would have thought this would be default behaviour. Spaces in image names just cause problems.

Thanks

murphy.adrian28211
murphy.adrian28211

Nobody? I'm new to October and frankly if I get no response to a question in a week then I'm not sure what to think.

Anyway I hacked together a solution that works for me. The core code is:

Event::listen( 'media.file.upload', function ( $widget, $filePath, $uploadedFile ) {

        $original_name  = $uploadedFile->getClientOriginalName();

        $ext     = pathinfo( $original_name, PATHINFO_EXTENSION );

        $original_name_no_ext = pathinfo( $original_name, PATHINFO_FILENAME );

        $new_name = str_slug( $original_name_no_ext, '-' ) . '.' . $ext;

        if ( $new_name == $original_name ) {
            return;
        }

        $newPath = str_replace( $original_name, $new_name, $filePath );
        MediaLibrary::instance()->moveFile( $filePath, $newPath );

    } );

Last updated

chocolata
chocolata

Hi, thank you for your code. After a recent update, I've also seen issues with spaces in files. I am also looking for a solution. Where did you place that snippet?

1-3 of 3

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.