PHP 增量文件名

发布于 2025-01-07 18:28:44 字数 1521 浏览 3 评论 0原文

我有这个检查文件名的函数。如果存在,它会按照以下模式将其增加 1:

image.jpg
image1.jpg
image2.jpg

问题出现在第四张图像上,它返回 0.jpg

这是相关代码:

...
$filetarget = $this->make_image_filename($directory, $new_filename, $extension);

if(!move_uploaded_file($file['tmp_name'], $filetarget)){
    $error[$index] = 'copy';
}
...

private function make_image_filename($directory, $name = '', $extension){
    if(empty($name)) $name = 'NULL';
    $filetarget = $directory.$name.$extension;

    if(file_exists($filetarget)){
        $name = $this->increment_filename($name);

        return $this->make_image_filename($directory, $name, $extension);
    } else {
        return $filetarget;
    }
}


private function increment_filename($name){

    $index = $this->get_filename_index($name);

    if(is_numeric($index)){
        $pos = strpos($name, $index);
        $name = substr($name, 0, $pos);
    }

    if(is_null($index)){
        $index = 0;
    }

    ++$index;

    return $name.$index;
}

private function get_filename_index($name){
    // CHECK FOR INDEX
    $i = 1;
    $index = substr($name, -$i);
    $last_chars = substr($name, -$i);

    while(is_numeric($last_chars)){
        ++$i;
        $last_chars = substr($name, -$i);
        if(is_numeric($last_chars)){
            $index = $last_chars;
        }
    }

    if(is_numeric($index)){
        return $index;
    } else {
        return NULL;
    }
}

我现在正在本地服务器上隔离此代码以运行一些测试。您能看出这个过程中存在什么固有缺陷吗?

I have this function that checks for a filename. If it exists, it increments it by one following this patter:

image.jpg
image1.jpg
image2.jpg

The problem comes on the 4th image, it comes back with 0.jpg.

Here is the relevant code:

...
$filetarget = $this->make_image_filename($directory, $new_filename, $extension);

if(!move_uploaded_file($file['tmp_name'], $filetarget)){
    $error[$index] = 'copy';
}
...

private function make_image_filename($directory, $name = '', $extension){
    if(empty($name)) $name = 'NULL';
    $filetarget = $directory.$name.$extension;

    if(file_exists($filetarget)){
        $name = $this->increment_filename($name);

        return $this->make_image_filename($directory, $name, $extension);
    } else {
        return $filetarget;
    }
}


private function increment_filename($name){

    $index = $this->get_filename_index($name);

    if(is_numeric($index)){
        $pos = strpos($name, $index);
        $name = substr($name, 0, $pos);
    }

    if(is_null($index)){
        $index = 0;
    }

    ++$index;

    return $name.$index;
}

private function get_filename_index($name){
    // CHECK FOR INDEX
    $i = 1;
    $index = substr($name, -$i);
    $last_chars = substr($name, -$i);

    while(is_numeric($last_chars)){
        ++$i;
        $last_chars = substr($name, -$i);
        if(is_numeric($last_chars)){
            $index = $last_chars;
        }
    }

    if(is_numeric($index)){
        return $index;
    } else {
        return NULL;
    }
}

I am in the process now of isolating this code on my local server to run some tests. Can you see anything inherently flawed in this process?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拔了角的鹿 2025-01-14 18:28:44

这是我用来做同样事情的函数:(

function make_unique($full_path) {
    $file_name = basename($full_path);
    $directory = dirname($full_path).DIRECTORY_SEPARATOR;

    $i = 2;
    while (file_exists($directory.$file_name)) {
        $parts = explode('.', $file_name);
        // Remove any numbers in brackets in the file name
        $parts[0] = preg_replace('/\(([0-9]*)\)$/', '', $parts[0]);
        $parts[0] .= '('.$i.')';

        $new_file_name = implode('.', $parts);
        if (!file_exists($new_file_name)) {
            $file_name = $new_file_name;
        }
        $i++;
    }
    return $directory.$file_name;
}

除了它使文件名像 image(1).jpg image(2).jpg 一样)

Here is a function I use to do the same thing:

function make_unique($full_path) {
    $file_name = basename($full_path);
    $directory = dirname($full_path).DIRECTORY_SEPARATOR;

    $i = 2;
    while (file_exists($directory.$file_name)) {
        $parts = explode('.', $file_name);
        // Remove any numbers in brackets in the file name
        $parts[0] = preg_replace('/\(([0-9]*)\)$/', '', $parts[0]);
        $parts[0] .= '('.$i.')';

        $new_file_name = implode('.', $parts);
        if (!file_exists($new_file_name)) {
            $file_name = $new_file_name;
        }
        $i++;
    }
    return $directory.$file_name;
}

(except it make file names like image(1).jpg image(2).jpg)

动听の歌 2025-01-14 18:28:44

这个怎么样:

function get_next_file_name($file) {
    if (!preg_match('/^(\D+)(\d*)(\.\S+)$/', $file, $match)) {
        throw new Exception('bad file name format');
    }
    return $match[1] . (empty($match[2]) ? 1 : ($match[2] + 1)) . $match[3];
}
echo get_next_file_name('image.jpg'), "\n";      // prints image1.jpg
echo get_next_file_name('image1.jpg'), "\n";     // prints image2.jpg
echo get_next_file_name('image999.jpg'), "\n";   // prints image1000.jpg

How about this:

function get_next_file_name($file) {
    if (!preg_match('/^(\D+)(\d*)(\.\S+)$/', $file, $match)) {
        throw new Exception('bad file name format');
    }
    return $match[1] . (empty($match[2]) ? 1 : ($match[2] + 1)) . $match[3];
}
echo get_next_file_name('image.jpg'), "\n";      // prints image1.jpg
echo get_next_file_name('image1.jpg'), "\n";     // prints image2.jpg
echo get_next_file_name('image999.jpg'), "\n";   // prints image1000.jpg
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文