Add wrapper around move_uploaded_file

This commit is contained in:
jrenslin committed 2021-07-20 01:26:31 +02:00
1 parent dbbdf4f230
commit 6adf0ee0a2
2 files changed
+40

No files matched your search

+19
View File
@@ -679,4 +679,23 @@ public static function string_to_color_code(string $str):string {
return $code;
}
/**
* Checks if a directory is writable.
*
* @param string $dir Directory path.
*
* @return void
*/
public static function check_is_writable(string $dir):void {
if (is_dir($dir) === false) {
throw new MDFileDoesNotExist("Directory " . $dir . " does not exist");
}
if (is_writable($dir) === false) {
throw new MDFileIsNotWritable("Directory " . $dir . " is not writable");
}
}
}
+21
View File
@@ -317,4 +317,25 @@ public static function ensureStringIsUtf8(string $input):string {
return $input;
}
/**
* Wrapper around move_uploaded_file that throws errors in case the upload failed
* for an identifiable reason.
*
* @param string $filename Name of the file to upload.
* @param string $destination Destination to move the file to.
*
* @return boolean
*/
public static function move_uploaded_file(string $filename, string $destination):bool {
MD_STD::check_is_writable(dirname($destination));
if (!(\move_uploaded_file($filename, $destination))) {
return false;
}
return true;
}
}