I'm using imce to give each user their own upload folder.
If their name has a space in it, the path to their files is /sites/files/upload/user%20name/

Obviously that isn't desirable. Is there any workaround?

At the moment I don't have any solution, even the most drastic one of not allowing spaces in user names; there doesn't seem to be any way of forcing that on registration.

Cheers for any advice.

Comments

ufku’s picture

If the directory name is "user name" then the URL should contain %20 instead of the space. This is the intended behavior. An URL should not contain spaces.
However, if you want to change the behavior, you can have a look at getURL method in imce.js file. You can add path.replace(/%20/g, ' ') to convert them back.

-Anti-’s picture

Thanks for the reply.

> You can add path.replace(/%20/g, ' ') to convert them back.

I don't want to convert the %20 back to spaces, but instead replace the spaces with
something else like the pathauto and transliteration modules do.

For instance:
'user name' becomes: user-name, user.name, or username when in a url.

The replacement for a space in a url on my site seems to be a hyphen.
So path.replace(/%20/g, '-') should work?

I'd actually prefer to not allow spaces in user names, but can't see how to disallow
using spaces during registration.

Cheers.

ufku’s picture

No it wont work because the URL becomes incorrect. And also there could be two users with the names "user name" and "user-name". You need to differentiate them.
There seems to be no solution on IMCE side. Did you search for a module that allow extra restrictions on usernames?

-Anti-’s picture

Status: Active » Closed (works as designed)

> Did you search for a module that allow extra restrictions on usernames?

Yes. There doesn't seem to be any.
There were about six posts asking about how to restrict spaces, but none were answered.

OK, thanks.

mittalpatel’s picture

I did a simple username validate hack to restrict space (multiple spaces in a row are already restricted.) in usernames.

Go to user.module file and find user_validate_name and replace
if (strpos($name, ' ') !== FALSE) return t('The username cannot contain multiple spaces in a row.');
with
if (strpos($name, ' ') !== FALSE) return t('The username cannot contain spaces.');

(line # 384 in drupal 6.10)

Now, it will pose an error if anyone try to register a username containing space.

Hope this will resolve your problem.

- Mittal Patel
http://www.MittalPatel.co.in

-Anti-’s picture

Thanks for that!

I wish there was a core option for admin-defined special characters in user-names.
Also what is missing is a separate login name and user nickname - that would be so handy (and more secure).
Perhaps these features will make it into D7 core.

KoCo’s picture

Another approach depending on token on pathauto, is using php code in a IMCE profile:

php: return 'users/'.pathauto_cleanstring(token_replace('[user]', 'user', $user));

but build in filter to prevent '-' or ' ' to be used in usernames, whichever you prefer.

jared12’s picture

This PHP approach is perfect. Except it doesn't work for me.

When I try to navigate to the IMCE interface I get this error:

Fatal error: Call to undefined function pathauto_cleanstring() in ...\sites\all\modules\imce\inc\page.inc(724) : eval()'d code on line 1
(The ellipses are mine.)

However, if I throw this code into the page.tpl.php file in this format:

print 'users/'.pathauto_cleanstring(token_replace('[user]', 'user', $user));

it prints the correct string. So, does this mean that IMCE is trying to resolve the user name before the pathauto_cleanstring function is declared?

(Sorry if my programming terminology is wrong.)

osopolar’s picture

Because this function is in a separate inc file (pathauto.inc). I think you need to include this file first. May use this function: _pathauto_include();

jared12’s picture

Fantastic! That does it. Thank you so much. Here's what I ended up using in the IMCE Directory Path field:

php: _pathauto_include(); return 'users/'.pathauto_cleanstring(token_replace('[user]', 'user', $user));

Between this tip and the Pathauto patch for enabling the lowercase setting in Pathauto to apply to the pathauto_cleanstring function, I have exactly what I need: clean, lowercase, user-based folder names. Beautiful!

Slovak’s picture

For anyone searching on this topic, in D7 I was able to convert user names to lower case and replaces spaces with a '-' by entering:

php: return strtolower('users/'.str_replace(' ','-',$user->name));

atiba’s picture

Thanks a lot Slovak! That was the exact thing I was looking for!
The only thing i needed to change is to use 'print' instead of 'return'.
Somehow 'return' didn't work out.

Thanks again!