Forums

Remove spaces from uploaded files name.

edebeer 15 Jan, 2012
I need to know how to remove spaces from a file for upload. When I link using the file name with spaces the link is sometimes broken. Spaces also need to be removed from the data loaded into the mysql database.
GreyHead 15 Jan, 2012
Hi edebeer,

You can probably do it by urlencoding the name, or by using a string replace to take out spaces.

Alternatively I guess it might be possible to add validation the the file upload box to disallow spaces there.

Bob
edebeer 15 Jan, 2012
I was able to remove the space with the attached code, but only for the name saved in the database how do I change the name of the uploaded file? The code was placed in On Success in Upload File.
<?php
$form->data['photo']= str_replace(" ","_",$form->data['photo']);
?>
GreyHead 15 Jan, 2012
Hi edebeer,

Well, yes that's running after the file is uploaded so you'd need to rename the file.

You might want to try my Upload File [GH] action that will let you use data from the form in the file name box so you could use a Custom Code action to create a urlencoded version of the file name, save it to the $form->data array and use that.

Bob

PS I'll look at adding a simple name cleaner into the action but it won't be today :-(
edebeer 15 Jan, 2012
Just one problem - I am using the new image resizer and there is no on success area to load it in. Anyway to make this work.
edebeer 16 Jan, 2012
I tried using Upload File [GH] action, instead I did the following.
I hacked upload_files.php on around line 82 I added:
$file_name= str_replace(" ","_",$file_name);

Is this ok or does it mess something up?
GreyHead 16 Jan, 2012
Hi edebeer,

That's fine as a workaround. Make a note for yourself that it will break when you next upgrade ChronoForms to a new release.

Bob
GreyHead 18 Jan, 2012
Hi edebeer,

If you scroll down the options in the Upload Files action you'll find 'Safe File name' right at the bottom.

Bob
This topic is locked and no more replies can be posted.