【问题标题】:How can i add a number to a file name in php如何在php中为文件名添加数字
【发布时间】: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的示例

标签: php regex file


【解决方案1】:

我假设您在这里指的是这组代码。

if(preg_match_all($target_file, "'('[0-9]{1,}')'")==false){
    $target_file= $target_path."(1)".$name;
}

insert the number before the extension of the file

编辑: 使用 explode() 并重新格式化分机。

示例:

$target_path = "/assets/imgages/";
$name = 'img.jpg';
$name = explode('.', $name);
$format = $name[0].'(1).'.$name[1];
$path = $target_path.$format;

将产生以下字符串:

/assets/img/notes(1).txt

接受字符串中的多个点。

$filename = 'company.jobtitle.field.text';  

function formatDuplicateExtension($filename){
    $stmt = NULL;
    $format = explode('.', $filename);
    $i = 0;
    foreach($format as $key => $value){     
        if($value === end($format)){
            $stmt .= '(1).'.$format[$i];
        }elseif($key === count($format)-2){
            $stmt .= $format[$i];
        }else{
            $stmt .= $format[$i].'.';
        }
    $i++;     
    }
return $stmt;
}

echo formatDuplicateExtension($filename);

$filename = 'company.jobtitle.field.text';

输出: //-->/assets/imgages/company.jobtitle.field(1).text

$name = 'trees.vac2012.img.jpg';

输出: //--> /assets/imgages/trees.vac2012.img(1).jpg

【讨论】:

  • 也许我现在没有正确解释自己(1).txt存在等等
  • 我的问题不在于更改名称我必须找到像“.ext”这样的字符串的最新出现并在像 (1) 之前插入一个字符串,如果它不存在或 (2) ( 3) (4) 如果文件存在则依此类推
  • 如果文件的名称中包含一个点,例如 file.name.txt?如果文件存在,我如何找到名为 filename(1).txt 的文件?
  • 您可以检查数组中有多少项,然后进行相应的编码。 'if(count($name) = 2){ // 使用迭代器分解和格式化 } 或检查是否
  • 好的,我会尝试感谢并找到(数字)发生率?我不能使用explode()...
【解决方案2】:

如果它是最好的,我已经找到了一个解决方案 idk,因为正则表达式搜索和替换涉及很多次,而且它们似乎是资源消耗函数。

//this function insert the $number in the name of the file before .extension
function InsertBeforeExtension($filename,$number){
    $stmt = NULL;
    $format = explode('.', $filename);
    $i = 0;
    foreach($format as $key => $value){     
        if($value === end($format)){
            $stmt .= '('.$number.').'.$format[$i];
        }elseif($key === count($format)-2){
            $stmt .= $format[$i];
        }else{
            $stmt .= $format[$i].'.';
        }
    $i++;     
    }
return $stmt;
}

//this function check if there's a string like (number).ext in the name
//if yes increment the (number) in the string that become (number++).ext
//if no insert (1) before .ext
function insertnumber($string){
    $matches=array();
    $re = '/[(][0-9]+[)]\.[a-zA-Z]+/m';
    preg_match_all($re, $string, $matches, PREG_SET_ORDER, 0);
    if($matches[0][0]){
        //if (number).ext is present
        $re = '/[(][0-9]+[)]/m';
        $str = $matches[0][0];
        //select the (number) only
        preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
        //remove parethesis
        $str=substr($matches[0][0],1,-1);
        //cast sting to an int for add a number
        $int = (int)$str;
        $int++;
        //replace the last (number) match in the name of the file with (number++)
        $re = '/(.*)[(][0-9]+[)]/m';
        $subst = '${1}('.$int.')';
        $result = preg_replace($re, $subst, $string);
    }else{
        //if (number).ext is not present insert (1) before .ext
        $result=InsertBeforeExtension($string,1);
    }
    return $result;
};

$target_file = $target_path.$name;
//If the file exist repeat to find the number of file that doesn't exist
        if( file_exists( $target_file )) {
            while(file_exists( $target_file )){
                $name=insertnumber($name);
                $target_file = $target_path.$name;
            }
        }

唯一的问题是,如果您上传了一个名为 file(3).txt 的文件,并且您上传了另一个同名文件,则此函数将其重命名为 file(4).txt 而不是 file(3)( 1).txt 但对我来说这并不重要

我已经对代码进行了注释,试图尽可能清楚地说明这个解决方案似乎运作良好,但我没有计算性能。

【讨论】:

    猜你喜欢
    • 2019-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-03
    • 2020-08-23
    • 2013-10-18
    • 1970-01-01
    相关资源
    最近更新 更多