Question

I have read on a PHP's website that file_put_contents is not atomic. I was planning to use that to upload a file.

I was then wondering about the atomicity of move_uploaded_file, but could not find any information.

Any help?

Thanks.

Was it helpful?

Solution

I'm assuming you mean atomic at a file-system level. It comes down to depending on your filesystem.

The process that happens with move_uploaded_files is basically:

  • Can files be uploaded?
  • Were two strings passed in?
  • Was the source file uploaded?
  • Does open_basedir allow writing to the destination?

Assuming all of those return a yes, then a rename is tried. On most filesystems, a rename is atomic.

If the rename failed, a copy and delete is then tried. This isn't atomic, as copying requires reading and then there's a separate delete.

Finally, if neither the rename nor the copy worked, an error is generated instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top