Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 665649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:43:53+00:00 2026-05-13T23:43:53+00:00

I use the following function to rename thumbnails. For example, if I upload a

  • 0

I use the following function to rename thumbnails.
For example, if I upload a file called “image.png” to an upload folder, and this folder already has a file named “image.png” in it, the new file automatically gets renamed to “image-copy-1.png”. If there also is a file called “image-copy-1.png” it gets renamed to “image-copy-2.png” and so on.
The following function returns the new filename. At least that’s what it is supposed to do…
The renaming doesn’t seeem to work correctly, though. Sometimes it produces strange results, like: (I always uploaded a file named “1.png”)
1-copy-1.png
1-copy-2.png
1-copy-2-copy-1.png
1-copy-2-copy-3.png

I hope you understand my problem, despite my description being somewhat complex… Can you tell me what went wrong here? (bonus question: Is regular expressions the right tool for doing this kind of stuff?)

<?php
function renameDuplicates($path, $file)
{   
    $fileName = pathinfo($path . $file, PATHINFO_FILENAME);
    $fileExtension = "." . pathinfo($path . $file, PATHINFO_EXTENSION);

    if(file_exists($path . $file))
    {
        $fileCopy = $fileName . "-copy-1";

        if(file_exists($path . $fileCopy . $fileExtension))
        {           
            if ($contains = preg_match_all ("/.*?(copy)(-)(\\d+)/is", $fileCopy, $matches))
            {
                $copyIndex = $matches[3][0];            
                $fileName = substr($fileCopy, 0, -(strlen("-copy-" . $copyIndex))) . "-copy-" . ($copyIndex + 1);
            }                       
        }

        else
        {
            $fileName .= "-copy-1";
        }
    }

    $returnValue = $fileName . $fileExtension;  
    return $returnValue;
}?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:43:54+00:00Added an answer on May 13, 2026 at 11:43 pm

    Simpler, without regex;

    function renameDuplicates($path, $file)
    {   
        $fileName = pathinfo($path . $file, PATHINFO_FILENAME);
        $fileExtension = "." . pathinfo($path . $file, PATHINFO_EXTENSION);
    
        $returnValue = $fileName . $fileExtension;
    
        $copy = 1;
        while(file_exists($path . $returnValue))
        {
            $returnValue = $fileName . '-copy-'. $copy . $fileExtension;
            $copy++;
        }
        return $returnValue;
    }
    

    Quick benchmark for performance issues (32000 is max files in folder):

    $start = microtime(1);
    $c=0;
    while($c<32000)
        if(file_exists(__FILE__))
            $c++;
    echo microtime(1) - $start; /* 0.44202709197998 */
    

    So less than half second for worst-case scenario. And for 100 copies – 0.0013940334320068 s. And as for regex:

    $start = microtime(1);
    $contains = preg_match_all ("/.*?(copy)(-)(\\d+)/is", __FILE__, $matches);
    echo microtime(1) - $start; /* 0.010906934738159 */ 
    

    so if you plan to have more than ~800 copies of one file regex will be faster (by some microseconds, but faster) 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 412k
  • Answers 412k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer _followersRepo.Load(_followersRepo.GetUsersFollowingIDQuery(CurrentUserId)); If this line is used to load data into… May 15, 2026 at 8:01 am
  • Editorial Team
    Editorial Team added an answer The first thing I see is that the last development… May 15, 2026 at 8:01 am
  • Editorial Team
    Editorial Team added an answer You're looping over all of the cells in each row… May 15, 2026 at 8:01 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.