Reputation: 328
$uploaddir = wp_upload_dir();
$file = $_FILES['panpdf'];
$uploadfile = $uploaddir['path'] . '/' . basename($file['name']);
move_uploaded_file($file['tmp_name'], $uploadfile);
$filename = basename($uploadfile);
$wp_filetype = wp_check_filetype(basename($filename), null);
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
'post_content' => '',
'post_status' => 'inherit',
'menu_order' => $_i + 1000
);
$update_img = wp_insert_attachment($attachment, $uploadfile);
This work fine and upload file but it upload duplicate, I want to add number at end of filename if filename already exist in upload directory. How I achieve this . Please help me.
Thanks in advance.
Upvotes: 3
Views: 99
Reputation: 450
Append the time stamp to you file name as per below -
$dateTime = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
$fileName = $file['name']."_".$dateTime;
And then pass this file name.
Upvotes: 5