0

I'm using:

{field:my-field-name}

to display my file upload form in Channel Forms to allow the user to upload a file.

I want to be able to change the filename and append their user ID to it but can't figure out how...the EE documentation doesn't provide any clues either.

Any thoughts?

Thanks, Dan

1

You'll need to write an extension which uses the file_after_save hook. This hook passes you the $file_id and an array of info about the file called $data. Using this info you should be able update the file name doing something like this in your extension:

function file_after_save($file_id, $data)
{
    $parts = pathinfo($data['file_name']);
    $new_file_name =
        $parts['filename'].
        '_foobar_'. // whatever you want to add to the filename
        $data['uploaded_by_member_id'].
        '.'.$parts['extension'];

    ee()->load->library('filemanager');
    ee()->filemanager->rename_file($file_id, $new_file_name, true);
}
2
  • 1
    Thanks for the response...I'll try the extension route and see if I have any luck...was hoping for it to be a simple solution since I've never written an extension before. Will mark as the answer as soon as I try it and have some luck. Thanks again!
    – DanH
    Jan 18 '15 at 21:39
  • I'm wondering if you ever managed to write this extension, because I'm very interested in achieving the same thing...
    – Tim
    Oct 7 '20 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.