note 124707 added to function.file-get-contents

From: Date: Tue, 11 Feb 2020 07:35:13 +0000
Subject: note 124707 added to function.file-get-contents
Groups: php.notes 
Request: Send a blank email to php-notes+get-214080@lists.php.net to get a copy of this message
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; } ?> ---- Server IP: 45.112.84.4 Probable Submitter: 185.85.3.195 (proxied: 94.236.211.70) ---- Manual Page -- http://php.net/manual/en/function.file-get-contents.php Edit -- https://master.php.net/note/edit/124707 Del: integrated -- https://master.php.net/note/delete/124707/integrated Del: useless -- https://master.php.net/note/delete/124707/useless Del: bad code -- https://master.php.net/note/delete/124707/bad+code Del: spam -- https://master.php.net/note/delete/124707/spam Del: non-english -- https://master.php.net/note/delete/124707/non-english Del: in docs -- https://master.php.net/note/delete/124707/in+docs Del: other reasons-- https://master.php.net/note/delete/124707 Reject -- https://master.php.net/note/reject/124707 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#214080) next »