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

  • Home
  • SEARCH
  • 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 70k
  • Answers 70k
  • 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 String.Join(',', myListOfStrings.ToArray()) May 11, 2026 at 1:05 pm
  • added an answer You can't initialize the size of an array with a… May 11, 2026 at 1:05 pm
  • added an answer You might want to check out this three part series… May 11, 2026 at 1:05 pm

Related Questions

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
Before you answer this I have never developed anything popular enough to attain high
I want to use the php simple HTML DOM parser to grab the image,
I have a PHP script (running on a Linux server) that ouputs the names
So I did not look at the right location before posting this.. I was

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.