在上传时通过帖子ID(woocommerce中的产品ID)更改媒体名称

How to change media and attachment name by post ID in wordpress?
I need a function to use in function.php to change media name on upload.
I've tested this code but it didn't work for me.

add_action('add_attachment', 'rename_attacment');
function rename_attacment($post_ID){

$post = get_post($post_ID);
$file = get_attached_file($post_ID);
$path = pathinfo($file);
    //dirname   = File Path
    //basename  = Filename.Extension
    //extension = Extension
    //filename  = Filename

$newfilename = "NEW FILE NAME HERE";
$newfile = $path['dirname']."/".$newfilename.".".$path['extension'];

rename($file, $newfile);    
update_attached_file( $post_ID, $newfile );

}

for each product I have 5 images that their naming is random like randomName.jpg , randomName2.jpg, ......... ; and I want to change their name on upload to post ID like this:
3314-0.jpg, 3314-1.jpg, 3314-2.jpg, 3314-3.jpg, 3314-4.jpg.