Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does PHP wait for filesystem operations (like file_put_contents) to complete before moving on?

I have a question about how PHP handles filesystem operations. I'm running this code that depends on a file being created before it gets used, and it feels like when I run the code it becomes a race condition - sometimes the it works, the file is created and php code uses it, sometimes it fails.

So I was wondering how php handles filesystem operations, does it send it off in the background or does it wait till the operation complete?

like image 889
Matt Avatar asked Jul 21 '10 23:07

Matt


People also ask

Does PHP wait for function to finish?

PHP is single-threaded, which means that it executes one instruction at a time before moving on. In other words, PHP naturally waits for a function to finish executing before it continues with the next statement.

What is File_put_contents?

The file_put_contents() function in PHP is an inbuilt function which is used to write a string to a file. The file_put_contents() function checks for the file in which the user wants to write and if the file doesn't exist, it creates a new file.

How can save file in specific folder in PHP?

php $target_Path = "images/"; $target_Path = $target_Path. basename( $_FILES['userFile']['name'] ); move_uploaded_file( $_FILES['userFile']['tmp_name'], $target_Path ); ?> when the file(image) is saved at the specified path... WHAT if i want to save the file with some desired name....

How create file if not exist in PHP?

PHP Create File - fopen() The fopen() function is also used to create a file. Maybe a little confusing, but in PHP, a file is created using the same function used to open files. If you use fopen() on a file that does not exist, it will create it, given that the file is opened for writing (w) or appending (a).


1 Answers

file_put_contents is equivalent to fopen, fwrite, fclose. fclose should ensure the file is fully flushed to disk.

like image 116
Matthew Flaschen Avatar answered Sep 20 '22 08:09

Matthew Flaschen