php上传目录文件名,php – 如果文件名存在于媒体上传目录中,则在文件名末尾添加数字...

$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);

这工作正常并上传文件,但它上传重复,如果文件名已存在于上传目录中,我想在文件名末尾添加数字.我如何实现这一目标.请帮我.

提前致谢.

解决方法:

将时间戳附加到您的文件名,如下所示 –

$dateTime = new DateTime('now', new DateTimeZone('Asia/Kolkata'));

$fileName = $file['name']."_".$dateTime;

然后传递此文件名.

标签:php,file-upload,wordpress

来源: https://codeday.me/bug/20190828/1747808.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值