Log in or register to post comments
Last post
SOLVED :::::::::::: Auto rename directory to remove spaces (
Hi,

I've been using CK Finder a while now and just realised that it doesnt automatically rename directories to remove spaces ... example - I can create a directory called 'flemmings images' .. which is fine and generally works - but I think there may be some circumstances where this is a problem? If not a problem it certainly (to me) looks a little unprofessional, to see '%20' in image paths!

I know I can put an underscore in myself rather than a space .. but the system I'm creating is for a bunch of teachers and I KNOW that despite my best efforts to train them they will forget of ignore my advice!

If there was some easy way to auto replace spaces in directory names I would LOVE to hear about it!

many thanks!

Flemming
Re: Auto rename directory to remove spaces (replace with undersc
The %20 behavior is absolutely correct. A URL shall not contain a space, it has to be escaped with %20. There is nothing you can do about except to modify CKFinder's code.
Re: Auto rename directory to remove spaces (replace with undersc
Just wanted to add that if you really want to remove all such characters automatically from folder names, you can quite easily do this by yourself.
All you need to do is to change a bit code of the server connector, which is located in the "core\connector" directory ("_source" in CKFinder for ASP.NET).

Wiktor Walc
CTO
--
CKSource - http://cksource.com
--
Follow CKEditor on: Twitter | Facebook | Google+

Re: Auto rename directory to remove spaces (replace with undersc
thanks guys! will take a look that connector (i'm using the PHP version) and post back here if I have any success!
Re: Auto rename directory to remove spaces (replace with undersc
SOLUTION!

If you're using PHP this is how you can auto rename directories (to stop users putting spaces in the directory name for example) ...

In: ckfinder > core > connector > php > php4 (or 5) > CommandHandler > CreateFolder.php look for :

$sNewFolderName = isset($_GET["NewFolderName"]) ? $_GET["NewFolderName"] : "";
$sNewFolderName = CKFinder_Connector_Utils_FileSystem::convertToFilesystemEncoding($sNewFolderName);


and add in these 3 rows in between

// replace spaces etc with underscore
$replacers = array(" ", "  ", "-", "!", ":", ";", "#", "@", "'");
$sNewFolderName = str_replace($replacers, "_", $sNewFolderName);


so it looks like this :

$sNewFolderName = isset($_GET["NewFolderName"]) ? $_GET["NewFolderName"] : "";
// replace spaces etc with underscore
$replacers = array(" ", "  ", "-", "!", ":", ";", "#", "@", "'");
$sNewFolderName = str_replace($replacers, "_", $sNewFolderName);
$sNewFolderName = CKFinder_Connector_Utils_FileSystem::convertToFilesystemEncoding($sNewFolderName);


and that's all there is to it! you can of course add whatever you need into the array that I've called 'replacers' - $replacers = array("foo", "bar", "helloworld"); and these will all be replaced with whatever character you specify in the str_replace.
Re: SOLVED :::::::::::: Auto rename directory to remove spaces (
I've also done something similar in .NET.

I added a simple replace function into the existing Util class to replace any non-url friendly characters (spaces and the like) To implement, modify the Util class in \Connector\Util.cs and add the following function (at the end will be fine)

/// <summary>
/// Takes a string and makes it into a URL friendly string
/// </summary>
/// <param name="title"></param>
/// <returns></returns>
/// <remarks></remarks>
public static string GetURLFriendly(string title)
{
    return Regex.Replace(title, "[^\\w_\\.-]+", "-");
}

You might also have to include a reference to System.Text.RegularExpressions;

Now, you have to edit two files, one for creating folders, the other for renaming
\Connector\CommandHandlers\CreateFolderCommandHandler.cs

Add the following under line 36 so it looks like this, making use of the new function you added to the Utils class:

string sNewFolderName = HttpContext.Current.Request.QueryString[ "newFolderName" ];
sNewFolderName = Util.GetURLFriendly(sNewFolderName);

and now for renaming folders, using
\Connector\CommandHandlers\RenameFolderCommandHandler.cs

Again this is at line 36:

string newFileName = Request[ "NewFolderName" ];
newFileName = Util.GetURLFriendly(newFileName);


Note you'll have to recompile the project and use the updated DLL for this to be effective.

I hope this is useful for somebody - it would be easy enough to add this for file uploads as well. I've actually added some custom code to mine that appends a few random characters to a non-image file name, eg filename.pdf becomes filename_ABC.pdf to prevent accidental over-writes, but thats maybe functionality that not everybody would use or require.
Re: SOLVED :::::::::::: Auto rename directory to remove spaces (
@psykoptic Thanks M8!!! I too have several clients who cant seem to follow simple directions like: "Dont use spaces in file names and or folder names..."
nifty little hack

This should be made standard in CKFinder

Now on to figuring out how to stop my clients from uploading images with spaces or weird characters...

wOOt

WebDude
Re: SOLVED :::::::::::: Auto rename directory to remove spaces (
WebDude wrote:@psykoptic Thanks M8!!! I too have several clients who cant seem to follow simple directions like: "Dont use spaces in file names and or folder names..."
nifty little hack

This should be made standard in CKFinder

Now on to figuring out how to stop my clients from uploading images with spaces or weird characters...

WebDude

The same approach for directories can be used for filenames, just find where the filename is created in the appropriate handler. For example

\Connector\CommandHandlers\FileUploadCommandHandler.cs

after line 45:
sFileName = System.IO.Path.GetFileName( oFile.FileName );
sFileName = Util.GetURLFriendly(sFileName );


\Connector\CommandHandlers\RenameFileCommandHandler.cs

after line 35:
string newFileName = Request["newFileName"];
newFileName = Util.GetURLFriendly(newFileName );


Note: THIS IS UNTESTED - I've just found the relevent lines, there may be something I've missed, but its pretty simple stuff, anybody who's been using .net for more than 5 minutes should be able to understand it.
Re: SOLVED :::::::::::: Auto rename directory to remove spac
Hey all,

Could anyone tell me how I do this for uploaded images?
At the moment the spaces are replaced by "%20" and it'd like it be an underscore.
The files do get uploaded correctly at the moment, but the URL wont work.

Whatever change I make in the sourcecode it never works in my project ... I rebuild and re-add the folder to my project ... nothing. What am I doing wrong?

Thanks in advance,
- Yannick
Re: SOLVED :::::::::::: Auto rename directory to remove spac
Yannick86 wrote:Hey all,

Could anyone tell me how I do this for uploaded images?
At the moment the spaces are replaced by "%20" and it'd like it be an underscore.
The files do get uploaded correctly at the moment, but the URL wont work.

Whatever change I make in the sourcecode it never works in my project ... I rebuild and re-add the folder to my project ... nothing. What am I doing wrong?

Thanks in advance,
- Yannick


Yannick,

In the following file:

Ckfinder -> Core -> PHP -> php5 -> CommandHandler -> FileUpload.php 


Go to line 58 and you will find this:

$sFileName = str_replace(array(":", "*", "?", "|", "/"), "_", $sUnsafeFileName);


Change it to this:

$sFileName = str_replace(array(":", "*", "?", "|", "/", " "), "_", $sUnsafeFileName);


This will replace the spaces for an underscore and, like in my custom CMS, will stop throwing errors on PHP commands such as getfilesize() and similar.

Hope this helps you mate,

George.
Re: SOLVED :::::::::::: Auto rename directory to remove spac
It would be nice if this function/feature could be added as a configuration option.
Re: SOLVED :::::::::::: Auto rename directory to remove spac
I actually went ahead and made this a configuration option. Simply add "CleanFileNames = true;" in your CKFinder config file and you'll be all set! CleanFileNames = false; is the default.

Attachments: 

Re: SOLVED :::::::::::: Auto rename directory to remove spac
I don't see how this is done in 2.3. In 2.3, FileUpload.php doesn't have that list of characters that should be pulled out of an uploaded file.

Ideas?

Thanks,

Shai Gluskin
Re: SOLVED :::::::::::: Auto rename directory to remove spac
In the v2.3, in core/connector/php/php5/Utils/FileSystem.php there is a secureFileName method, where you can add your characters that should be stripped out from the filename.
Line 331:
$fileName = str_replace(array(":", "*", "?", "|", "/"), "_", $fileName);