Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Hiya Cezar,

    You can use PHP’s copy() function ??
    Can be used for any file on your server, media file or not.
    https://www.php.net/manual/en/function.copy.php

    Thread Starter Cezar Ayran

    (@ayrancd)

    @bcworkz but how am I going to add it to the media? Duplicate the file and the media info in the database.

    I am using media_handle_upload() to upload the file.

    Moderator bcworkz

    (@bcworkz)

    I suppose you could use the ‘wp_generate_attachment_metadata’ filter. Your callback will be passed all pertinent information about the current upload. In the callback, you could use copy() to make a copy of the media file, then call wp_insert_attachment() and wp_generate_attachment_metadata() again, this time passing the copy’s data.

    Before making that call, your callback should remove itself from the call stack. Otherwise you’ll get into an infinite loop situation where copies are made of copies are made of copies… etc.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Duplicate media file using php’ is closed to new replies.