How can I rename files uploaded via Safecracker?

More specifically, I would like to prefix the file name with the logged in user's username, convert to lower case & replace spaces with hyphens.

I was using safecracker_file_add_prefix

However, it no longer works since I upgraded from 2.4.0 to 2.5.3


2 Answers 2


I believe this is due to a new change in the safecracker/libraries/safecracker_lib.php file for 2.5.3. It seems to be causing a lot of issues (like preventing the use of Safecracker in a Matrix field). Here's a forum link: http://ellislab.com/forums/viewthread/224822/P18/#1040436

Go to line 230 in safecracker_lib.php and you'll see the following:

        if (isset($_FILES[$field['field_name']]['name']))
            $_POST[$field['field_name']] = $_FILES[$field['field_name']];

Comment out $_POST[$field['field_name']] = $_FILES[$field['field_name']];

        if (isset($_FILES[$field['field_name']]['name']))
            /* HACK ppohler 11/13/2012, fix issue with Safecracker inside of Matrix */
            //$_POST[$field['field_name']] = $_FILES[$field['field_name']]['name'];
            /* HACK */

Now safecracker_add_prefix should work (as well as Matrix). Keep in mind you'll want to log that you've made this change in core somewhere in case you need to update EE.

  • Thanks Patrick! I tried commenting out the line mentioned. However, now the file upload field doesn't validate & therefore the form does not get submitted & file is not uploaded. Is there something I can change in the safecracker_file_add_prefix file? Any other suggestions or ways to accomplish same goal? Nov 27, 2012 at 20:34
  • Andrea, did you manage to get this working? About to start a project where there will be many user submitted images/files and need a way to rename upon upload. Mar 1, 2013 at 0:52
  • I'd also like to know if this was achieved? I'm allowing people to upload a logo via safecracker, and would ideally like to have it renamed.
    – shorn
    Apr 5, 2013 at 10:42
  • I never did get this working. I don't have the programming skills necessary to modify the plugin so that is works in EE 2.5.3. Can anyone help? Apr 23, 2013 at 16:46

Rob has an updated version for Channel Form here: https://github.com/rsanchez/channel_form_file_add_prefix/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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