45 $lng = $DIC->language();
47 $dirlist = opendir($dir);
48 while (
false !== ($file = readdir($dirlist))) {
49 if (!is_file($dir .
"/" . $file) && !is_dir($dir .
"/" . $file)) {
51 $lng->txt(
"filenames_not_supported"),
56 if ($file !=
'.' && $file !=
'..') {
57 $newpath = $dir .
'/' . $file;
58 $level = explode(
'/', $newpath);
59 if (is_dir($newpath)) {
62 $arr[
"path"][] = $dir .
"/";
63 $arr[
"file"][] = end($level);
76 return $DIC->refinery()->string()->encoding()->latin1ToUtf8()->transform($string);
86 $DIC->fileServiceSettings()
89 return $sanitizer->
sanitize($a_filename);
95 public static function rename(
string $a_source,
string $a_target): bool
97 $pi = pathinfo($a_target);
100 $DIC->fileServiceSettings()
103 if (!$sanitizer->isClean($a_target)) {
107 return rename($a_source, $a_target);
130 bool $preserveTimeAttributes =
false 132 $sourceFS = LegacyPathHelper::deriveFilesystemFrom($a_sdir);
133 $targetFS = LegacyPathHelper::deriveFilesystemFrom($a_tdir);
135 $sourceDir = LegacyPathHelper::createRelativePath($a_sdir);
136 $targetDir = LegacyPathHelper::createRelativePath($a_tdir);
139 if (!$sourceFS->hasDir($sourceDir)) {
143 $sourceList = $sourceFS->listContents($sourceDir,
true);
145 foreach ($sourceList as $item) {
146 if ($item->isDir()) {
150 $itemPath = $targetDir .
'/' . substr(
154 $stream = $sourceFS->readStream($item->getPath());
155 $targetFS->writeStream($itemPath, $stream);
186 $a_dir = dirname($a_dir);
188 while ($last_dirname != $a_dir) {
189 array_unshift($dirs, $a_dir);
190 $last_dirname = $a_dir;
191 $a_dir = dirname($a_dir);
195 $reverse_paths = array_reverse($dirs,
true);
197 foreach ($reverse_paths as
$key => $value) {
198 if ($found_index == -1) {
199 if (is_dir($value)) {
205 $old_mask = umask(0000);
206 foreach ($dirs as $dirindex => $dir) {
208 if ($dirindex >= $found_index) {
209 if (!file_exists($dir)) {
210 if (strcmp(substr($dir, strlen($dir) - 1, 1),
"/") == 0) {
213 $dir = substr($dir, 0, strlen($dir) - 1);
216 error_log(
"Can't make directory: $dir");
219 } elseif (!is_dir($dir)) {
220 error_log(
"$dir is not a directory");
224 $umask = fileperms($dir);
259 if (!is_dir($directory)) {
261 $size = @filesize($directory);
262 return ($size ===
false) ? -1 : $size;
264 if ($DIR = opendir($directory)) {
265 while (($dirfile = readdir($DIR)) !==
false) {
267 $directory . DIRECTORY_SEPARATOR . $dirfile
268 ) || $dirfile ==
'.' || $dirfile ==
'..') {
271 if (is_file($directory . DIRECTORY_SEPARATOR . $dirfile)) {
273 $directory . DIRECTORY_SEPARATOR . $dirfile
275 } elseif (is_dir($directory . DIRECTORY_SEPARATOR . $dirfile)) {
277 $directory . DIRECTORY_SEPARATOR . $dirfile
309 public static function makeDir(
string $a_dir): bool
311 $a_dir = trim($a_dir);
314 if (substr($a_dir, -1) ==
"/") {
315 $a_dir = substr($a_dir, 0, -1);
319 if (!(
$path = substr(
322 strrpos($a_dir,
"/") - strlen($a_dir)
328 if (is_dir($a_dir)) {
331 $old_mask = umask(0000);
332 $result = @mkdir($a_dir, fileperms(
$path));
355 case strpos($a_target, ILIAS_ABSOLUTE_PATH .
'/Customizing') === 0:
360 "Can not move files to \"$a_target\" because path can not be mapped to web, storage or customizing location." 364 $absTargetDir = dirname($a_target);
365 $targetDir = LegacyPathHelper::createRelativePath($absTargetDir);
367 return [$targetFilesystem, $targetDir];
393 bool $a_raise_errors =
true,
394 string $a_mode =
"move_uploaded" 397 $main_tpl = $DIC->ui()->mainTemplate();
398 $target_filename = basename($a_target);
403 [$target_filesystem, $target_dir] = self::sanitateTargetPath($a_target);
405 $upload = $DIC->upload();
408 if (!$upload->hasBeenProcessed()) {
413 if (!$upload->hasUploads()) {
415 $DIC->language()->txt(
"upload_error_file_not_found")
418 $upload_result = $upload->getResults()[$a_file] ?? null;
420 if (!$upload_result->isOK()) {
421 throw new ilException($upload_result->getStatus()->getMessage());
427 if (!$a_raise_errors) {
428 $main_tpl->setOnScreenMessage(
'failure', $e->getMessage(),
true);
436 $upload->moveOneFileTo(
450 public static function zip(
453 bool $compress_content =
false 457 return $DIC->legacyArchives()->zip($a_dir, $a_file,
true);
475 public static function delDir(
string $a_dir,
bool $a_clean_only =
false): void
477 if (!is_dir($a_dir) || is_int(strpos($a_dir,
".."))) {
481 $current_dir = opendir($a_dir);
490 while ($entryname = readdir($current_dir)) {
491 $files[] = $entryname;
494 foreach ($files as $file) {
497 ) and ($file !=
"." and $file !=
"..")) {
499 } elseif ($file !=
"." and $file !=
"..") {
500 unlink($a_dir .
"/" . $file);
504 closedir($current_dir);
505 if (!$a_clean_only) {
512 $file_peaces = explode(
'.', $a_initial_filename);
514 $file_extension = array_pop($file_peaces);
516 if (SUFFIX_REPL_ADDITIONAL) {
517 $string_extensions = SUFFIX_REPL_DEFAULT .
"," . SUFFIX_REPL_ADDITIONAL;
519 $string_extensions = SUFFIX_REPL_DEFAULT;
522 $sufixes = explode(
",", $string_extensions);
524 if (in_array($file_extension, $sufixes)) {
525 $file_extension =
"sec";
528 $file_peaces[] = $file_extension;
531 foreach ($file_peaces as $piece) {
532 $safe_filename .=
"$piece";
533 if ($piece != end($file_peaces)) {
534 $safe_filename .=
".";
538 return $safe_filename;
559 ?
string $a_sub_dir =
"" 561 $current_dir = opendir($a_dir . $a_sub_dir);
566 while ($entry = readdir($current_dir)) {
567 if (is_dir($a_dir .
"/" . $entry)) {
568 $dirs[$entry] = [
"type" =>
"dir",
570 "subdir" => $a_sub_dir
572 if ($a_rec && $entry !=
"." && $entry !=
"..") {
576 $a_sub_dir .
"/" . $entry
578 $subitems = array_merge($subitems, $si);
581 if ($entry !=
"." && $entry !=
"..") {
582 $size = filesize($a_dir . $a_sub_dir .
"/" . $entry);
583 $files[$entry] = [
"type" =>
"file",
586 "subdir" => $a_sub_dir
594 return array_merge($dirs, $files, $subitems);
614 if ($mode ===
"filesystem") {
617 if (defined(
"ILIAS_MODULE")) {
645 $size =
new DataSize(self::getPhpUploadSizeLimitInBytes(), DataSize::MB);
646 $max_filesize = $size->__toString();
647 $lng = $DIC->language();
649 return $lng->txt(
"file_notice") .
" $max_filesize.";
659 return $policy->
ascii($a_filename);
669 public static function ilTempnam(?
string $a_temp_path = null): string
671 if ($a_temp_path === null) {
674 $temp_path = $a_temp_path;
677 if (!is_dir($temp_path)) {
680 $temp_name = $temp_path .
"/" . uniqid(
"tmp");
693 public static function unzip(
string $a_file,
bool $overwrite =
false,
bool $a_flat =
false): bool
695 if(defined(
'DEVMODE') && DEVMODE) {
696 trigger_error(
'Deprecated method called: ' . __METHOD__, E_USER_DEPRECATED);
700 return $DIC->legacyArchives()->unzip(
714 $def_arr = explode(
",", SUFFIX_REPL_DEFAULT);
715 foreach ($def_arr as $def) {
716 self::rRenameSuffix($a_dir, trim($def),
"sec");
719 $def_arr = explode(
",", SUFFIX_REPL_ADDITIONAL);
720 foreach ($def_arr as $def) {
721 self::rRenameSuffix($a_dir, trim($def),
"sec");
731 public static function rRenameSuffix(
string $a_dir,
string $a_old_suffix,
string $a_new_suffix): bool
733 if ($a_dir ==
"/" || $a_dir ==
"" || is_int(strpos($a_dir,
".."))
734 || trim($a_old_suffix) ==
"") {
739 if (!@is_dir($a_dir)) {
744 $dir = opendir($a_dir);
746 while ($file = readdir($dir)) {
747 if ($file !==
"." && $file !==
"..") {
749 if ($file ===
'...') {
750 unlink($a_dir .
"/" . $file);
754 if (@is_dir($a_dir .
"/" . $file)) {
759 if (@is_file($a_dir .
"/" . $file)) {
761 if (strrpos($file,
'.') == (strlen($file) - 1)) {
763 rename($a_dir .
'/' . $file, substr($a_dir .
'/' . $file, 0, -1));
766 unlink($a_dir .
'/' . $file);
769 $file = substr($file, 0, -1);
772 $path_info = pathinfo($a_dir .
"/" . $file);
774 if (strtolower($path_info[
"extension"] ??
'') === strtolower($a_old_suffix)) {
775 $pos = strrpos($a_dir .
"/" . $file,
".");
776 $new_name = substr($a_dir .
"/" . $file, 0, $pos) .
"." . $a_new_suffix;
778 if (file_exists($new_name)) {
779 if (is_dir($new_name)) {
785 rename($a_dir .
"/" . $file, $new_name);
795 $path = preg_replace(
"/[\/\\\]+$/",
"", $path);
796 return (
string)
$path;
804 $convertPhpIniSizeValueToBytes =
function ($phpIniSizeValue) {
805 if (is_numeric($phpIniSizeValue)) {
806 return $phpIniSizeValue;
809 $suffix = substr($phpIniSizeValue, -1);
810 $value = substr($phpIniSizeValue, 0, -1);
812 switch (strtoupper($suffix)) {
834 $uploadSizeLimitBytes = min(
835 $convertPhpIniSizeValueToBytes(ini_get(
'post_max_size')),
836 $convertPhpIniSizeValueToBytes(ini_get(
'upload_max_filesize'))
839 return $uploadSizeLimitBytes;
static getWebspaceDir(string $mode="filesystem")
get webspace directory
Class ilFileServicesPolicy.
const STORAGE
The filesystem outside of the ilias web root.
static utf8_encode(string $string)
Class ChatMainBarProvider .
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
This class provides the data size with additional information to remove the work to calculate the siz...
static unzip(string $a_file, bool $overwrite=false, bool $a_flat=false)
unzip file
static rCopy(string $a_sdir, string $a_tdir, bool $preserveTimeAttributes=false)
Copies content of a directory $a_sdir recursively to a directory $a_tdir.
static getValidFilename(string $a_filename)
static rRenameSuffix(string $a_dir, string $a_old_suffix, string $a_new_suffix)
Renames all files with certain suffix and gives them a new suffix.
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
static removeTrailingPathSeparators(string $path)
static getSafeFilename(string $a_initial_filename)
static getASCIIFilename(string $a_filename)
static renameExecutables(string $a_dir)
static recursive_dirscan(string $dir, array &$arr)
Recursively scans a given directory and writes path and filename into referenced array.
static dirsize(string $directory)
get size of a directory or a file.
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static createDirectory(string $a_dir, int $a_mod=0755)
create directory
const CUSTOMIZING
The filesystem within the web root where all the skins and plugins are saved.
static getDir(string $a_dir, bool $a_rec=false, ?string $a_sub_dir="")
get directory
static moveUploadedFile(string $a_file, string $a_name, string $a_target, bool $a_raise_errors=true, string $a_mode="move_uploaded")
move uploaded file
static getDataDir()
get data directory (outside webspace)
static sanitateTargetPath(string $a_target)
static _sanitizeFilemame(string $a_filename)
Class ilFileServicesFilenameSanitizer.
static ilTempnam(?string $a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
sanitize(string $filename)
static zip(string $a_dir, string $a_file, bool $compress_content=false)
const TEMPORARY
The ILIAS temporary directory.
static getPhpUploadSizeLimitInBytes()
static rename(string $a_source, string $a_target)
const WEB
The filesystem within the ilias web root.
static makeDir(string $a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...