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

Asked
Active3 hr before
Viewed126 times

6 Answers

90%

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?, I wonder why file_put_contents does not have a option to flush file to immediatly write it on disk. – John Boe Oct 9 '19 at 14:37 ,file_put_contents is equivalent to fopen, fwrite, fclose. fclose should ensure the file is fully flushed to disk., About

Accidentally 5ms is about the full committed write time of a high quality hdd.

file_put_contents($filename, 'abcdefghi...~100chars', FILE_APPEND);
load more v
88%

https://stackoverflow.com/questions/7054844/is-rename-atomic has further references to what you might need to do if you are operating across filesystems.,An issue you didn't mention that you also need to be careful of is race conditions where two instances of your script are running at the nearly the same time, for example this order of occurrences:, Student asked me if it is necessary to simplify fractions at the end of answering a question. I'm not sure how to respond , Help Center Detailed answers to any questions you might have

Step 3) & 4) would then become this:

$tempfile = uniqid(microtime(true)); // make sure we have a unique name
file_put_contents($tempFile); // write temp file
rename($tempfile, 'original.txt'); // ideally on the same filesystem
load more v
72%

file_put_contents — Write data to a file,file_get_contents() - Reads entire file into a string, This function returns the number of bytes that were written to the file, or false on failure. ,A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.

load more v
65%

Under some special circumstances, you might be appending data to a file from multiple scripts at the same time. In these situations, it is advisable to get an exclusive lock on the file using the LOCK_EX flag. This can help prevent data corruption or some other unexpected behavior. When you use this flag, other scripts will wait for the current process to complete writing to the file before they append their own data.,In the above example, we wrote some strings to a file called canada.txt which contains information about Canada. Both the string were appended at the end of the file one after the other.,Now that the file is open, we can use the fwrite() function to add information to the file. fwrite() takes three parameters:, length: is optional and it is used to set the maximum number of bytes that should be written to the file.

Here is an example in which some text is appended to an existing file using file_put_contents().

< ? php

// Original File: Canada is a country in North America. .... bi-national land border.

// File Contents After this Line: Canada is a country in North America. .... bi-national land border.  Canada's capital is Ottawa,
file_put_contents('canada.txt', " Canada's capital is Ottawa,", FILE_APPEND | LOCK_EX);

// File Contents After this Line: Canada is a country in North America. .... bi-national land border.  Canada's capital is Ottawa, and its three largest metropolitan areas are Toronto, Montreal, and Vancouver.
file_put_contents('canada.txt', " and its three largest metropolitan areas are Toronto, Montreal, and Vancouver.", FILE_APPEND | LOCK_EX);

?
>

Here is an example that shows you how to use fopen(), fwrite(), and fclose() to append data to a file.

< ? php

//open the file
$square_file = fopen("squares.txt", "a+");

//write the squares from 1 to 10
for ($i = 1; $i <= 10; $i++) {
   $square = $i * $i;
   $cube = $square * $i;
   $line = "Square of $i is: $square.\n";
   fwrite($square_file, $line);
}

//read the first line of the file and echo
fseek($square_file, 0);
echo fgets($square_file);

//close the file
fclose($square_file);

?
>
Square of 1 is: 1.
Square of 2 is: 4.
Square of 3 is: 9.
Square of 4 is: 16.
Square of 5 is: 25.
Square of 6 is: 36.
Square of 7 is: 49.
Square of 8 is: 64.
Square of 9 is: 81.
Square of 10 is: 100.
load more v
75%

$data[] = $_POST['data'];

$inp = file_get_contents('results.json');
$tempArray = json_decode($inp);
array_push($tempArray, $data);
$jsonData = json_encode($tempArray);
file_put_contents('results.json', $jsonData);
load more v
40%

The code for downloading the images is quite similar to the previous examples, making use of file_put_contents to write the files to the local filesystem, as the library does not provide a method to do that.,Let’s start with the options available as part of PHP's core functionality and extensions.,Note: I’ve omitted the constants to shorten the example.,As streams are part of PHP’s core, you don’t have to do much to make use of their functionality. What’s more, they integrate with many of PHP’s core functions, such as fopen and file_get_contents. Consequently, you don’t need to install a third-party library or custom extension to start using it.

mkdir - p php - http / photos
cd php - http
composer require vlucas / phpdotenv
load more v

Other "undefined-undefined" queries related to "Does PHP wait for filesystem operations (like file_put_contents) to complete before moving on?"