From 413324cf427c82f0c3c1904f98856bb0dae6254e Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Thu, 8 Sep 2016 09:05:17 +0000 Subject: [PATCH] file : add md5_file and sha1_file support git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3029 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- file.php | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/file.php b/file.php index 301b156..99bc48b 100644 --- a/file.php +++ b/file.php @@ -296,6 +296,28 @@ class file return $res; } + + /** Calculate the md5 sum of a file + * @param $filename The file to hash + * @return the calulated hash + * @throws If the file doesn't exists + */ + public function md5_file ($filename) + { + $this->debug (2, "md5_file ($filename)"); + $filename = $this->realpath ($filename); + $this->checkPathRO (dirname ($filename)); + if (! is_file ($filename)) + throw new \Exception (sprintf (dgettext ("domframework", + "File '%s' is not a file"), + $filename), 500); + if (! is_readable ($filename)) + throw new \Exception (sprintf (dgettext ("domframework", + "File '%s' is not readable"), + $filename), 500); + return md5_file ($filename); + } + /** Create a new directory * @param $pathname The directory to create * @param $mode The mode to create (0777 by default) @@ -517,6 +539,27 @@ class file return $res; } + /** Calculate the sha1 sum of a file + * @param $filename The file to hash + * @return the calulated hash + * @throws If the file doesn't exists + */ + public function sha1_file ($filename) + { + $this->debug (2, "sha1_file ($filename)"); + $filename = $this->realpath ($filename); + $this->checkPathRO (dirname ($filename)); + if (! is_file ($filename)) + throw new \Exception (sprintf (dgettext ("domframework", + "File '%s' is not a file"), + $filename), 500); + if (! is_readable ($filename)) + throw new \Exception (sprintf (dgettext ("domframework", + "File '%s' is not readable"), + $filename), 500); + return sha1_file ($filename); + } + /** Create a new file or update the timestamp if the file exists * @param string $filename the filename * @param int $time the timestamp to use (actual timestamp if not defined)