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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:08:01+00:00 2026-05-10T21:08:01+00:00

When you use the PHP copy function, the operation blindly copies over the destination

  • 0

When you use the PHP copy function, the operation blindly copies over the destination file, even if it already exists. How do you copy a file safely, only performing the copy if there is no existing file?

  • 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. 2026-05-10T21:08:02+00:00Added an answer on May 10, 2026 at 9:08 pm

    The obvious solution would be to call file_exists to check to see if the file exists, but doing that could cause a race condition. There is always the possibility that the other file will be created in between when you call file_exists and when you call copy. The only safe way to check if the file exists is to use fopen.

    When you call fopen, set the mode to ‘x’. This tells fopen to create the file, but only if it doesn’t exist. If it exists, fopen will fail, and you’ll know that you couldn’t create the file. If it succeeds, you will have a created a file at the destination that you can safely copy over. Sample code is below:

    // The PHP copy function blindly copies over existing files.  We don't wish // this to happen, so we have to perform the copy a bit differently.  The // only safe way to ensure we don't overwrite an existing file is to call // fopen in create-only mode (mode 'x').  If it succeeds, the file did not // exist before, and we've successfully created it, meaning we own the // file.  After that, we can safely copy over our own file.  $filename = 'sourcefile.txt' $copyname = 'sourcefile_copy.txt' if ($file = @fopen($copyname, 'x')) {     // We've successfully created a file, so it's ours.  We'll close     // our handle.     if (!@fclose($file)) {         // There was some problem with our file handle.         return false;     }      // Now we copy over the file we created.     if (!@copy($filename, $copyname)) {         // The copy failed, even though we own the file, so we'll clean         // up by itrying to remove the file and report failure.         unlink($copyname);         return false;     }      return true; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 67k
  • Answers 67k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Unfortunately what you want to do is impossible without bringing… May 11, 2026 at 11:48 am
  • added an answer The simplest and most fun way (imo) is glob foreach… May 11, 2026 at 11:48 am
  • added an answer Have you considered writing a frontend for GCC? I mention… May 11, 2026 at 11:48 am

Related Questions

When you use the PHP copy function, the operation blindly copies over the destination
When you use the WebBrowser control in .NET you can embed an instance of
When you use the POISSON function in Excel (or in OpenOffice Calc), it takes
mono creates its own debug targets called .mdb files when you use the mcs
Duplicate post, see: When do you use the "this" keyword? On almost every project
When you use a Windows Forms TextBox, the default number of tab stops (spaces)
As the title states when would you recommend the use of the mediator design
What is the difference between const and readonly in C#? When would you use
The very common beginner mistake is when you try to use a class property
Can you use calculated fields in Excel 2007 pivot tables when the data source

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.