Forums

Rename file on upload

Warren.Kessler 03 Jul, 2016
I have recently updated to CF5 and rebuilt most of my forms. I am trying to rebuild a form with a file upload action. In CF4 I used GH custom actions to rename the file base on a field name from the form. {FirstName}-{LastName}-{datetime}.

I'm thinking I need to use the File name code box to accomplish this now, but I cant figure out how.
GreyHead 04 Jul, 2016
Hi Warren,

Please try this in the File Name code box:
<?php
return $form->data['FirstName'].'-'.$form->data['LastName'].'-'.date('YmdHis');
?>

Bob
Warren.Kessler 04 Jul, 2016
Thanks Bob, that works.

Is there a way to include the original extension? I tried to figure this out on my own but no luck.

$file_name (works but returns full name with extension, but I would like only extension)

So I tried:
[list]
$file_ext
$file['extension']
$pathinfo['extension']
$path_parts['extension']
[/list]
none return anything

Thanks,
Warren
GreyHead 04 Jul, 2016
Hi Warren,

Use a Custom Code action after the Upload Files action with code like this
<?php
jimport('joomla.filesystem.file');
$form->data['ext'] = JFile::getExt($form->data['file_input_name']);
?>

Bob
Dollique 24 Jul, 2016
I had the renaming problem with V5 too and I found a really simple solution that should work for most problems.

In the Upload field action go to the new field called "File name code".

If you use this, it will return exactly what your file should be renamed to.

I will give you a full example with file extension here:
<?php
$path = $_FILES['char_img']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);
return "upl_file.".$ext;
?>


Very easy and with this you can do most renaming in a really simple way!
This topic is locked and no more replies can be posted.