• Resolved jishua

    (@jishua)


    If two files are uploaded with the same name, the plugin seems to allow both files to keep that name. The normal media library behavior is to add a number before the extension for a duplicated name.

    Is this a case we need to handle on our own using something like wp_unique_filename?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author mirceatm

    (@mirceatm)

    Yes, you are correct.
    You may want to subscribe to nmr_create_attachment_file_name filter, call wp_unique_filename and change the file name before the file is saved.

    Thread Starter jishua

    (@jishua)

    Thanks a lot for confirming. Just wondering if there’s a reason that’s not implemented.

    I’m happy to contribute that update if you like. Just let me know.

    Plugin Author mirceatm

    (@mirceatm)

    I’ve briefly consider it, now when you mentioned it.
    Looking at the code, something is not yet clear to me:

    1. user uploads a file in CF7
    2. this plugin gets the filename
    3. should we change the filename here, if the filename already exists in the media library?
    4. raise this event/filter: nmr_create_attachment_file_name – send the potentially new filename to subscribers – remember this might not be the same filename the user uploaded. Does it matter? I don’t know, but it’s solvable by everyone with a bit of code as pointed above

    So, I haven’t implemented it yet. Please consider the reach of this change, how it might affect other websites using it and if you are confident you may contribute the solution.

    Thread Starter jishua

    (@jishua)

    That’s a very good point. I wouldn’t want to break anything for other users of the plugin if they’re always expecting the same filename. Maybe an option to enable it at some point.

    Hello!

    You may want to subscribe to nmr_create_attachment_file_name filter, call wp_unique_filename and change the file name before the file is saved.

    Any clue about how to do that? I have not PHP/wordpress experience and don’t know how to apply the filter/subscribers mechanism.
    Thanks in advance.

    Plugin Author mirceatm

    (@mirceatm)

    Hello!
    Answering this is a bit out of scope of this plugin and this thread.
    Here is a ‘quick and dirty’ untested solution:
    – edit a file called functions.php on your active theme
    – add code:

    function on_nmr_create_attachment_file_name($filename)
    {
    $path_parts?=?pathinfo($filename); //get details about file name
    //make sure that file name is unique and return it.
    return wp_unique_filename($path_parts[‘dirname’], $path_parts[‘filename’]);
    }
    add_filter(‘nmr_create_attachment_file_name’, ‘on_nmr_create_attachment_file_name’);

    • This reply was modified 1 year, 11 months ago by mirceatm.
    • This reply was modified 1 year, 11 months ago by mirceatm.
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Files Uploaded with Same File Name Issue’ is closed to new replies.