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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:14:20+00:00 2026-06-13T16:14:20+00:00

If a directory does not exist, i create the directory and save a file,

  • 0

If a directory does not exist, i create the directory and save a file, i can also just save a file if the directory exists. One file should be created at a time not all at the same time as the code below does. If the first file is created now then the next should only be created when the function is called. The files are Named R1,R2….Rn. How can I achieve this, This creates them all at the same time

$fileName1=$fileName1='somedir/'.$thedir.'/'.$thefile.'_R1.xlsx';

...

if (!dir($dirName)) 
{
    mkdir('somedir/' . thedir, 0777);
    $objWriter->save($fileName);
}

if (dir($dirName) && (!file_exists($fileName))) 
{
    $objWriter->save($fileName);
} 

if (dir($dirName) && file_exists($fileName)) 
{
    $objWriter->save('somedir/' . $thedir . '/' . $thefile . '_R1.xlsx');
}

if (dir($dirName) && file_exists($fileName1)) 
{
    $objWriter->save('somedir/' . $thedir . '/' . $thefile . '_R2.xlsx');
}

...
  • 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-06-13T16:14:21+00:00Added an answer on June 13, 2026 at 4:14 pm
    if (!is_dir($dir))
      mkdir($dir, 0777);
    
    $suffixes = array('_R1.xlsx', '_R2.xlsx');
    foreach($suffixes as $suffix) {
      $fileName = $dir.'/' . $thefile . $suffix;
      if (! file_exists($fileName)) {
        $objWriter->save($fileName);
        break;
      }
    }
    

    You have to add error handling, and note that just checking for a file can create concurrency issues (two processes trying both seeing that the file doesn’t exist and try to create it).

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

Sidebar

Related Questions

I use FileWriter for create a file. I have an error Directory does not
I want to create a directory if it does not exist already. Is using
I want to create a sub-directory of a directory that does not exist like:
First look at this url: https://stackoverflow.com/questions/tagged/xoxoxo/ This directory does not exists but somehow stackoverflow
Assignment of NSFileOwnerAccountName and NSFileGroupOwnerAccountName does not work? The directory created defaults to my
The following code allows me to create a directory if it does not already
I am using this line to create a new file (the file does not
The TextMate2 (Alpha 9090) terminal support does not install anything in any directory. Is
I have the feedparser.py in my project directory but my code does not work
I have a directory on my webserver. It does not have an index page.

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.