note 124707 deleted from function.file-get-contents by salathe

From: Date: Mon, 21 Sep 2020 14:38:39 +0000
Subject: note 124707 deleted from function.file-get-contents by salathe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-215243@lists.php.net to get a copy of this message
Note Submitter: vechenjivot (at) gmail (dot) com ---- It took me some time to realize that file_get_contents() will still read files successfully which were deleted with unlink() or in some other way (PHP 7.3.14 on Linux Apache). The same applies to fopen() and flock() which will also succeed. I was using this function as a wrapper for file_get_contents() to support locking and skipping the is_file() for better performance: <?php private function file_get_contents(string $filename, int $operation = LOCK_SH) { $contents = false; $handle = @fopen($filename, 'r'); if ($handle !== false) { if (flock($handle, $operation)) { $contents = @file_get_contents($filename); flock($handle, LOCK_UN); } fclose($handle); } return $contents; } ?> which returned the contents of previously deleted files, until I made a call to is_file() to check if the file exists first: <?php private function file_get_contents(string $filename, int $operation = LOCK_SH) { $contents = false; if (is_file($filename) && ($handle = @fopen($filename, 'r')) !== false) { if (flock($handle, $operation)) { $contents = @file_get_contents($filename); flock($handle, LOCK_UN); } fclose($handle); } return $contents; } ?>

« previous php.notes (#215243) next »