图像名称增加(如果已存在) [英] Increment image name if already exists

查看:100
本文介绍了图像名称增加(如果已存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码格式来更改图像名称(如果它已经存在于服务器上).如果要使用相同的名称,则需要增量图像名称.

例如: abc.png abc_1.png abc_2.png

function file_newname($path, $filename){
    if ($pos = strrpos($filename, '.')) {
       $name = substr($filename, 0, $pos);
       $ext = substr($filename, $pos);
    } else {
       $name = $filename;
    }

    $newpath = $path.'/'.$filename;
    $newname = $filename;
    $counter = 1;
    while (file_exists($newpath)) {
       $newname = $name .'_'. $counter . $ext;
       $newpath = $path.'/'.$newname;
       $counter++;
    }

    return $newname;
}

如果图片名称为 abc.png ,则上述代码有效.如果服务器上已经存在名称为 abc_1.png 的图像,并且我运行代码,它将生成图像格式为abc_1_1.png. 预期产量: 如果已经存在 abc_1.png 并且我运行代码$newname应该返回 abc_2.png .我该如何实现?

解决方案

您只需要检查字符串的结尾,而无需使用正则表达式.利用弱输入和 is_numeric 来查找任何值前一个计数器.

function file_newname($path, $filename){
    if ($pos = strrpos($filename, '.')) {
        $name = substr($filename, 0, $pos);
        $ext = substr($filename, $pos);
    } else {
        $name = $filename;
    }

    $newpath = $path.'/'.$filename;
    $newname = $filename;
    // New code here:
    if(file_exists($newpath)){
        $counter = 1;
        if($pos = strrpos($name, '_')) {
            $oldcounter = substr($name, $pos + 1);
            if(is_numeric($oldcounter)){
                $counter = $oldcounter + 1;
                $name = substr($name, 0, $pos);
            }
        }
        $newname = $name . '_' . $counter . $ext;
        $newpath = $path . '/' . $newname;

        while (file_exists($newpath)) {
            $newname = $name .'_'. $counter . $ext;
            $newpath = $path.'/'.$newname;
            $counter++;
        }
    }

    return $newname;
}

我已经对该功能进行了一些简化,因此可以将其引入实际示例中,但是如果您想使用我添加的替换部分中的代码,可以在此处进行验证: 解决方案

You just need to check what the string ends with, no need to use a regex for that. Take advantage of weak typing and is_numeric to find the value of any previous counter.

function file_newname($path, $filename){
    if ($pos = strrpos($filename, '.')) {
        $name = substr($filename, 0, $pos);
        $ext = substr($filename, $pos);
    } else {
        $name = $filename;
    }

    $newpath = $path.'/'.$filename;
    $newname = $filename;
    // New code here:
    if(file_exists($newpath)){
        $counter = 1;
        if($pos = strrpos($name, '_')) {
            $oldcounter = substr($name, $pos + 1);
            if(is_numeric($oldcounter)){
                $counter = $oldcounter + 1;
                $name = substr($name, 0, $pos);
            }
        }
        $newname = $name . '_' . $counter . $ext;
        $newpath = $path . '/' . $newname;

        while (file_exists($newpath)) {
            $newname = $name .'_'. $counter . $ext;
            $newpath = $path.'/'.$newname;
            $counter++;
        }
    }

    return $newname;
}

I've simplified the functionality a bit so I could get it into a live example, but If you want to play around with the code in the replacement section that I added you can check it out here: http://ideone.com/xR1v0j Just modify the initial value of $name to see how it will react to different inputs.

这篇关于图像名称增加(如果已存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆