Buy Now
Sign in

"Safe File name" in CF v5?

adrian99 , May 11 2015, 14:21
adrian99 10
May 11 2015, 14:21 #346997
In Chronoforms v4, the File Upload action had a field called "Safe File name" which we could set to Yes or No. This appears to have gone in v5. How can I cleanse a file name?

I notice that there is a new field called "File name code". Must we now provide our own cleansing code? If so, could someone give an example?

Many thanks.
GreyHead 64
May 12 2015, 09:47 #347018
Hi adrian99,

I hadn't noticed that it wasn't there any more :-(

The old CFv4 version used this
$file_name = JFile::makeSafe($file_post['name']);
but this does not remove spaces as far as I can see. Here is the function:
   * Makes file name safe to use
   * @param string $file The name of the file [not full path]
   * @return string The sanitised string
   * @since 11.1
  public static function makeSafe($file)
    // Remove any trailing dots, as those aren't ever valid file names.
    $file = rtrim($file, '.');

    $regex = array('#(\.){2,}#', '#[^A-Za-z0-9\.\_\- ]#', '#^\.#');

    return trim(preg_replace($regex, '', $file));

My Upload Files [GH] action used this function:
* source:
* author: Matteo Spinelli
* MIT License / (please re-check at source)
function toSlug($str, $replace=array(), $delimiter='-')
setlocale(LC_ALL, 'en_US.UTF8');
if( !empty($replace) ) {
$str = str_replace((array)$replace, ' ', $str);
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower(trim($clean, '-'));
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);

return $clean;
, or there is a more comprehensive version on Google Code here.

Whichever does the cleaning you need, you'd use a Custom Code action after the Upload Files action to create the new name, then 'move' the file to rename it; then update $form->files with the new information.

ChronoForms technical support
If you'd like to buy me a coffee or two, thank you very much
adrian99 10
May 12 2015, 12:16 #347030
Many thanks for a very comprehensive answer.
admin 29
May 12 2015, 21:24 #347044

This setting is available in v5 and its hardcoded, you can't disable it!

If your main question got answered then please mark the answer using the button!
Did you try the new ChronoForms7 ? if yes then please send us your comments!!