其实我在用uniqid(); 生成随机名称:
$file = UPLOAD_DIR . uniqid() . '.png';
输出如下:
53bd02cdc6b9b.png
53bd02cdc6bd8.png
53bd0320aafbc.png
53bd0320aaff7.png
53bd03e89b8df.png
我想将每个文件的这些名称更改为输出:
picture_0001.png
picture_0002.png
picture_0003.png
picture_0004.png
picture_0005.png
你有更好的主意吗?
您想要将所有当前图像修复为正确的格式(请参阅@FélixGagnon-Grenier答案),然后完成此操作后,您可以执行以下操作:
//get array of current images
$imgs = glob(UPLOAD_DIR.'*.png');
//select last image in array, strip out all non-alpha's then pad it with 4 0's
$next = str_pad(preg_replace("/[^0-9]/","", end($imgs))+1, 4, "0", STR_PAD_LEFT);
$file = UPLOAD_DIR.'picture_'.$next.'.png';
查看评论-基于文件的计数器
$count_file = UPLOAD_DIR.'count.txt'; //or put somewhere else
//make count file if not exists
if(!file_exists($count_file)){
file_put_contents($count_file,0);
}
//get last image count + 1
$next = file_get_contents($count_file)+1;
//set file var
$file = UPLOAD_DIR.'picture_'.sprintf("%04s",$next).'.png';
//update counter
file_put_contents($count_file, $next);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句