I have a script that loads posts automatically from a predefined array and uploads images as featured images from a physical path on a local machine.
When I copy the file into upload directory I use path and filename like so:
$upload_dir['path'].'/'.$new_post_id.$filename
This way if I upload apple.jpg several times, the file will be named 1apple.jpg, 2apple.jpg and so on and it will not be overwritten.
I would however like to use WordPress native functions that check and rename files. In wordpress uploads if you upload apple.jpg several times it will add a new number before the extension like so: apple1.jpg, apple2.jpg and so on, and the number portion is not based on a post.
I was wondering if you could point me to the function in wordpress that does this check and creates new filename so I can use the wordpress native functions for naming my uploaded files.
You need to filter the function
wp_unique_post_slug()
.As discussed in WPSE.
I found the answer: wp_unique_filename()