PHP Code to Increment Filename if already exists

By default, PHP replaces uploaded file with file exists with the same name. You may lose old files if it is not taken care of.

In this tutorial, you will learn how to increment filename if a file already exists in PHP. Therefore, we will keep both files if the file already exists.

Solution: We need to rename by appending increment number to the filename if file exists using PHP file_exists() function.

Code description

  1. We have created a separate function (passing $file_info array) to use it for multiple forms.
  2. In tc_file_upload() function, we have assigned upload files information to file.
  3. We extracted file name without extension so that we can increment if needed
  4. Save this as an original filename variable to track while renaming if the file already exists
  5. Start a counter $num = 1
  6. Check if file exists. If yes, increment filename by the number
  7. Check again and repeat until you get false using PHP file_exists() function.
  8. Keep storing filename with increment number, so that latest filename can be used to save the file. (when the same file does not exist).

Here is the code to rename the file (with increment by 1) again and again until we found existing file. Comments are mentioned with each code block so that you can easily understand the logic.


// Function to Upload file and auto-increment if file already
function tc_file_upload($file_info)

    // Get file/image name (with extension)
    $file_name_complete =  $file_info['name'];

    // Extract file extension
    $extension = pathinfo($file_name_complete, PATHINFO_EXTENSION);

    // Extract file name without extension
    $file_name = pathinfo($file_name_complete, PATHINFO_FILENAME);

    // Temp file location
    $file_temp_location =  $file_info['tmp_name'];

    // Save an original file name variable to track while renaming if file already exists
    $file_name_original = $file_name;

    // Increment file name by 1
    $num = 1;

     * Check if the same file name already exists in the upload folder, 
     * append increment number to the original filename
    while (file_exists("files/" . $file_name . "." . $extension)) {
        $file_name = (string) $file_name_original . $num;
        $file_name_complete = $file_name . "." . $extension;

    // Upload file in upload folder
    $file_target_location = "files/" . $file_name_complete;
    $file_upload_status = move_uploaded_file($file_temp_location, $file_target_location);

    if ($file_upload_status == true) {
        //echo "Congratulations. File has been uploaded to: $file_target_location";
        return $file_name_complete;
    } else {
        // echo "Error. File uploading failed! Check if 'upload' folder exists with proper permission and Try again.";
        // print_r(error_get_last());
        return false;

if (isset($_POST['submit'])) {

    // Check if file is selected to upload, else show error
    if (!empty($_FILES['user_file']['name'])) {
        $file_path = tc_file_upload($_FILES['user_file']);
        echo "<p>File uploaded: $file_path <p>";
    } else {
        echo "<p>No file found! Please upload a file<p>";

<!DOCTYPE html>

    <form action="" method="post" enctype="multipart/form-data">
        Upload File: <input type="file" name="user_file" required />
        <input type="submit" name="submit">


Important points to remember while file uploading fails:

  1. Make sure that you have created ‘files’ directory/folder where uploaded files will be stored.
  2. Please verify file permission in the above folder
  3. You can uncomment print_r(error_get_last()); function to see error details while uploading

Learn more about the similar topics:
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.