From 66a5b77b51649e8555d6c3fcca11b2d54dc2eff0 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Fri, 14 Apr 2023 14:13:09 +0200 Subject: [PATCH] Add function unlink_if_exists --- src/MD_STD.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/MD_STD.php b/src/MD_STD.php index 425ea68..e41f98e 100644 --- a/src/MD_STD.php +++ b/src/MD_STD.php @@ -94,6 +94,25 @@ final class MD_STD { } + /** + * Wrapper around unlink, that explicitly deletes files when existent, but + * accepts if they don't. An error is thrown if the files could not be + * deleted for other reasons. + * + * @see https://www.php.net/manual/en/function.unlink.php + * + * @param non-empty-string $filename File path. + * + * @return void + */ + public static function unlink_if_exists(string $filename):void { + + if (!\file_exists($filename)) return; + + self::unlink($filename); + + } + /** * Gets contents of a folder. *