【发布时间】:2020-04-19 15:46:55
【问题描述】:
大家晚安,如何在 php.ini 文件名中添加数字。 让我解释;我想使用 dropzone 保存文件,但如果该文件存在于文件夹中,我想重命名该文件。 我已经写下了这段代码,但正则表达式不起作用,如果可以像谷歌浏览器那样在文件扩展名之前插入数字。
if(file_exists($target_file)){
if(preg_match_all($target_file, "'('[0-9]{1,}')'")==false){
$target_file= $target_path."(1)".$name;
}else{
$pos=preg_match_all($target_file, "'('[0-9]{1,}')'");
$pos=$pos++;
$pos1=strpos($pos, $target_file, ")");
$pos1=$pos1-$pos;
$num=substr($target_file, $pos, $pos1);
$num = (int)$num;
$num =$num++;
$sostituisci="(".$num.")";
$target_file=preg_replace("'('[0-9]{1,}')'", $sostituisci, $target_file);
}
}
$name 是我想用扩展名保存的文件的名称 代码的第一个 $target_file 包含完整路径 + 文件名
$target_file 是一个类似于 /dropzone/upload/filename.txt 的字符串,而 $name 是一个类似于 filename.txt 的字符串。如果 $targetfile 存在,我将重命名 $name,如 filename(1).txt 或 filename(2).txt 等等
也接受其他解决方案,如 js 库。
【问题讨论】:
-
请展示
$target_file和$name的示例