Check if File Exists / Append Number to Name

If the file name exists, returns new file name with _number appended so you don't overwrite it.

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 = 0;
    while (file_exists($newpath)) {
           $newname = $name .'_'. $counter . $ext;
           $newpath = $path.'/'.$newname;
           $counter++;
     }

    return $newname;
}

Example returns:

myfile.jpg
myfile_0.jpg
myfile_1.jpg

Comments

  1. User Avatar
    Anton
    Permalink to comment#

    Strange this one doesn’t work on my site.

    My goal is to restrict users to upload only 10 pics each (from 1 to 10) … picture_1-10.jpg.
    If a user decided to delete a picture, for example picture_7.jpg and upload a new one the picture should be renamed with the same title as the deleted one.

    Any suggestions? Thanks in advance.

    Here is my PHP script:

    function Upload_GenFilename($filename, $tag, $filecounter = NULL)
      {
        $filedate = bqSearch;
        //Neuen Startwert f?r Zufallszahl bestimmen
        mt_srand();
        $randomnumber = mt_rand(1, 10);
    
        //Suffix = $tag mit dem . entfernen
        //nur wenn in filename enthalten
        if(stristr($filename,$tag))
        {
          $filename = substr($filename,0,strlen($filename)-strlen($tag)-1);
        }
    
        //Neuer Filename
        if($filecounter == NULL)
        {
          $newfilename = $filename.'_'.$filedate.'.'.$tag;
        }
    
        else
        {
          $newfilename = $filename.'_'.$filecounter.'_'.$filedate.'_'.$tag;
        }
        return $newfilename;
  2. User Avatar
    Gianluca
    Permalink to comment#

    If the filename has no extension, then php will complain that $$ext variable is not set.
    You should change this code:

     } else {
    	$name = $filename;
    }
    

    with this:

     } else {
    	 $name = $filename;
             $ext = "";
    }
    
  3. User Avatar
    Nadimuthu
    Permalink to comment#

    How to download my file from already uploaded path?. What function i will used.? please help me

  4. User Avatar
    Bhumi
    Permalink to comment#

    Thanks for sharing. useful to me.

  5. User Avatar
    sagar vispute
    Permalink to comment#

    Thanks to share the Logic, it is very useful……

  6. User Avatar
    Ivelin
    Permalink to comment#

    This will not work well if you upload an already renamed file.
    E.g. : if you upload “myfile_1.jpg” it will rename it to “myfile_1_1.jpg”.

    You have to check for “_NUMBER” in the file and increment that number.

Submit a Comment

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

icon-anchoricon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag