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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:10:49+00:00 2026-05-13T12:10:49+00:00

i want to create a new file in a folder with existing files with

  • 0

i want to create a new file in a folder with existing files with names in a numerical order eg. 1, 2, 3, 4…

i want to check what is the last nr and then create a file with one nr over that one.

i know i should use file_exists but i don’t know exactly how to use it, in a for loop maybe? but how?

would be nice if someone could give me a hint

  • 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-13T12:10:49+00:00Added an answer on May 13, 2026 at 12:10 pm

    I think this is your best bet (see revisions for previous versions):

    $files    = glob('/path/to/dir/*');      // get all files in folder
    natsort($files);                         // sort
    $lastFile = pathinfo(array_pop($files)); // split $lastFile into parts
    $newFile  = $lastFile['filename'] +1;    // increase filename by 1
    
    if(file_exists("/path/to/dir/$newFile")) { // do not write file if it exists
        die("$newFile aready exists");
    }
    file_put_contents("/path/to/dir/$newFile", 'stuff');  // write new file    
    

    As long as your filenames in the folder start with numbers, this should always write the highest numbered filename incremented by one, e.g.

    1,5,10                  => writes file 11
    1.txt, 5.gif, 10.jpg    => writes file 11
    1, 5.txt, 10_apple.txt  => writes file 11
    

    If there is a file not starting with a number, the above approach won’t work, because numbers are sorted before characters and thus nothing would be written for e.g.

    1,5,10,foo => foo+1 equals 1, already exists, nothing written
    

    You can get around this by changing the pattern for glob to /path/[0-9]*, which would then only match files starting with a number. That should be pretty solid then.

    Note natsort behaves different on different OS. The above works fine on my Windows machine, but you will want to check the resulting sort order to get it working for your specific machine.

    See the manual for further info on how to use glob(), natsort() and pathinfo();

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

Sidebar

Related Questions

I want to create a file named new text document.txt in the folder %tv%
I want to create a new object using Class::DBI. One of the fields of
I want to create a batch file that moves all the files in this
I want to create a text file into that folder that I am creating
I just want to create a File object like this File myImageFile = new
I am on Ubuntu10. Create new db file with touch development.db Want my migrations
In a C# program, I am creating files. I want to delete one file
I want to create some files at WEB-INF/upload and upload is a folder which
I want to create new node of BeanTreeView, and when I add some node
I'm using Visual Basic 9 (VS2008) I want to create new Tabs as 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.