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 7687211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:39:13+00:00 2026-05-31T19:39:13+00:00

My idea is like if there is a exist file , add the filename

  • 0

My idea is like if there is a exist file , add the filename to filename.(1).html and so on,
here is my code.But I can not figure out how to check the file name if exist have (number)
.
So how to check it and append a (1)… if that is exist?

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.1 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
</head>
<body>

<?php

session_start();
if($_SERVER['REQUEST_METHOD'] == "POST"){
$editorData = $_POST[ 'editor1' ];
$fileName = $_POST[ 'tname' ];
$uid= $_SESSION['username'];

$filePath ="../template/".$uid."/";
$myFile = $fileName.".html";

if (!file_exists($filePath)) {
mkdir($filePath, 0755);
}

if (file_exists($filePath.$myFile)) {
$myFile = $fileName."(".$num.")".html";
}



$fh = fopen($filePath.$myFile, 'w') or die("Can not open file");
fwrite($fh, $editorData);
fclose($fh);

echo "You have created a template. You can close this tab now";

}

?>
</body>
</html>
  • 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-31T19:39:14+00:00Added an answer on May 31, 2026 at 7:39 pm

    You can use glob to find all files matching your filename and the following number, you could then count those files to figure out what the next file should be named.

    Since your first file does not follow the same naming scheme as the following, you have to count that one for it self.

    // Check if a file with the exact name exists
    if(file_exists($filePath.$myFile)){
        // It does - the next number should be 1
        $num = 1;
        // Check how many more files with the name followed by a number exists
        $existingFiles = glob($filePath.$fileName.'([0-9]*).html');
        $num += count($existingFiles);
        // Create the new name of the file
        $myFile = $fileName."(".$num.")".html";
    }
    

    Be aware that if you allow deletion of the files, this will not work, as the count of existing files is no longer guaranteed to be equal to the highest number in the list.

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

Sidebar

Related Questions

I can't seem to find an answer out there for this. Here's the scenario:
I really like the idea of automatic binding generation like SWIG does. But it
Here is the idea I would like to develop. I need to have an
Given a name $path = 'articles/001.html'; I can check if such a file exists
This is not a CSS problem. The HTML isn't there. I need the menu,
I'm interested in learning about parallel programming in C#.NET (not like everything there is
Are these obsolete? They seem like the worst idea ever -- embed something in
I like the idea of abstracting CellControllers in a heterogeneous UITableView as suggested by
idea I would like to create a little app for myself to store ideas
Hi I'm new to Linux and I really like the idea of writing and

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.