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)