$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);
这个工作正常并上传文件,但它上传重复,如果文件名已存在于上传目录中,我想在文件名末尾添加数字。我如何实现这一目标。请帮帮我。
提前致谢。
答案 0 :(得分:5)
将时间戳附加到您的文件名,如下所示 -
$dateTime = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
$fileName = $file['name']."_".$dateTime;
然后传递此文件名。