52 $dirlist = opendir($dir);
53 while (
false !== ($file = readdir($dirlist))) {
54 if (!is_file($dir .
"/" . $file) && !is_dir($dir .
"/" . $file)) {
56 $lng->txt(
"filenames_not_supported"),
61 if ($file !==
'.' && $file !==
'..') {
62 $newpath = $dir .
'/' . $file;
63 $level = explode(
'/', $newpath);
64 if (is_dir($newpath)) {
67 $arr[
"path"][] = $dir .
"/";
68 $arr[
"file"][] = end($level);
81 return $DIC->refinery()->string()->encoding()->latin1ToUtf8()->transform($string);
91 $DIC->fileServiceSettings()
94 return $sanitizer->sanitize($a_filename);
100 public static function rename(
string $a_source,
string $a_target): bool
102 $pi = pathinfo($a_target);
105 $DIC->fileServiceSettings()
108 if (!$sanitizer->isClean($a_target)) {
112 return rename($a_source, $a_target);
135 bool $preserveTimeAttributes =
false
138 $targetFS = LegacyPathHelper::deriveFilesystemFrom($a_tdir);
140 $sourceDir = LegacyPathHelper::createRelativePath($a_sdir);
141 $targetDir = LegacyPathHelper::createRelativePath($a_tdir);
144 if (!$sourceFS->hasDir($sourceDir)) {
148 $sourceList = $sourceFS->listContents($sourceDir,
true);
150 foreach ($sourceList as $item) {
151 if ($item->isDir()) {
155 $itemPath = $targetDir .
'/' . substr(
159 $stream = $sourceFS->readStream($item->getPath());
160 $targetFS->writeStream($itemPath, $stream);
191 $a_dir = dirname($a_dir);
193 while ($last_dirname !== $a_dir) {
194 array_unshift($dirs, $a_dir);
195 $last_dirname = $a_dir;
196 $a_dir = dirname($a_dir);
200 $reverse_paths = array_reverse($dirs,
true);
202 foreach ($reverse_paths as $key => $value) {
203 if ($found_index != -1) {
206 if (!is_dir($value)) {
212 $old_mask = umask(0000);
213 foreach ($dirs as $dirindex => $dir) {
215 if ($dirindex >= $found_index) {
216 if (!file_exists($dir)) {
217 if (strcmp(substr($dir, strlen($dir) - 1, 1),
"/") == 0) {
220 $dir = substr($dir, 0, strlen($dir) - 1);
223 error_log(
"Can't make directory: $dir");
226 } elseif (!is_dir($dir)) {
227 error_log(
"$dir is not a directory");
231 $umask = fileperms($dir);
266 if (!is_dir($directory)) {
268 $size = @filesize($directory);
269 return ($size ===
false) ? -1 : $size;
271 if ($DIR = opendir($directory)) {
272 while (($dirfile = readdir($DIR)) !==
false) {
274 $directory . DIRECTORY_SEPARATOR . $dirfile
278 if ($dirfile ===
'.') {
281 if ($dirfile ===
'..') {
284 if (is_file($directory . DIRECTORY_SEPARATOR . $dirfile)) {
286 $directory . DIRECTORY_SEPARATOR . $dirfile
288 } elseif (is_dir($directory . DIRECTORY_SEPARATOR . $dirfile)) {
290 $directory . DIRECTORY_SEPARATOR . $dirfile
322 public static function makeDir(
string $a_dir): bool
324 $a_dir = trim($a_dir);
327 if (str_ends_with($a_dir,
"/")) {
328 $a_dir = substr($a_dir, 0, -1);
335 strrpos($a_dir,
"/") - strlen($a_dir)
336 )) ===
'' || (
$path = substr(
339 strrpos($a_dir,
"/") - strlen($a_dir)
345 if (is_dir($a_dir)) {
348 $old_mask = umask(0000);
349 $result = @mkdir($a_dir, fileperms(
$path));
357 $target_file_system = match (
true) {
364 str_starts_with($a_target,
CLIENT_DATA_DIR .
"/temp") => Location::TEMPORARY,
367 str_starts_with($a_target, ILIAS_ABSOLUTE_PATH .
'/public/Customizing') => Location::CUSTOMIZING,
368 default =>
throw new InvalidArgumentException(
369 "Can not move files to \"$a_target\" because path can not be mapped to web, storage or customizing location."
373 $absolute_target_dir = dirname($a_target);
374 $target_dir = LegacyPathHelper::createRelativePath($absolute_target_dir);
376 return [$target_file_system, $target_dir];
402 bool $a_raise_errors =
true,
403 string $a_mode =
"move_uploaded"
406 $main_tpl =
$DIC->ui()->mainTemplate();
407 $target_filename = basename($a_target);
412 [$target_filesystem, $target_dir] = self::sanitateTargetPath($a_target);
414 $upload =
$DIC->upload();
417 if (!$upload->hasBeenProcessed()) {
422 if (!$upload->hasUploads()) {
424 $DIC->language()->txt(
"upload_error_file_not_found")
427 $upload_result = $upload->getResults()[$a_file] ??
null;
429 if (!$upload_result->isOK()) {
430 throw new ilException($upload_result->getStatus()->getMessage());
436 if (!$a_raise_errors) {
437 $main_tpl->setOnScreenMessage(
'failure',
$e->getMessage(),
true);
445 $upload->moveOneFileTo(
459 public static function zip(
462 bool $compress_content =
false
466 return $DIC->legacyArchives()->zip($a_dir, $a_file,
true);
484 public static function delDir(
string $a_dir,
bool $a_clean_only =
false): void
486 if (!is_dir($a_dir) || is_int(strpos($a_dir,
".."))) {
490 $current_dir = opendir($a_dir);
499 while ($entryname = readdir($current_dir)) {
500 $files[] = $entryname;
503 foreach ($files as $file) {
506 ) && ($file !==
"." && $file !==
"..")) {
508 } elseif ($file !==
"." && $file !==
"..") {
509 unlink($a_dir .
"/" . $file);
513 closedir($current_dir);
514 if (!$a_clean_only) {
521 $file_peaces = explode(
'.', $a_initial_filename);
523 $file_extension = array_pop($file_peaces);
525 if (SUFFIX_REPL_ADDITIONAL) {
526 $string_extensions = SUFFIX_REPL_DEFAULT .
"," . SUFFIX_REPL_ADDITIONAL;
528 $string_extensions = SUFFIX_REPL_DEFAULT;
531 $sufixes = explode(
",", $string_extensions);
533 if (in_array($file_extension, $sufixes)) {
534 $file_extension =
"sec";
537 $file_peaces[] = $file_extension;
540 foreach ($file_peaces as $piece) {
541 $safe_filename .=
"$piece";
542 if ($piece != end($file_peaces)) {
543 $safe_filename .=
".";
547 return $safe_filename;
568 ?
string $a_sub_dir =
""
570 $current_dir = opendir($a_dir . $a_sub_dir);
575 while ($entry = readdir($current_dir)) {
576 if (is_dir($a_dir .
"/" . $entry)) {
580 "subdir" => $a_sub_dir
582 if ($a_rec && $entry !==
"." && $entry !==
"..") {
586 $a_sub_dir .
"/" . $entry
588 $subitems = array_merge($subitems, $si);
590 } elseif ($entry !==
"." && $entry !==
"..") {
591 $size = filesize($a_dir . $a_sub_dir .
"/" . $entry);
596 "subdir" => $a_sub_dir
603 return array_merge($dirs, $files, $subitems);
623 if ($mode ===
"filesystem") {
626 if (defined(
"ILIAS_MODULE")) {
652 $size =
new DataSize(self::getPhpUploadSizeLimitInBytes(), DataSize::MB);
653 $max_filesize = $size->__toString();
656 return $lng->txt(
"file_notice") .
" $max_filesize.";
666 return $policy->ascii($a_filename);
676 public static function ilTempnam(?
string $a_temp_path =
null): string
680 if (!is_dir($temp_path)) {
683 $temp_name = $temp_path .
"/" . uniqid(
"tmp");
696 public static function unzip(
string $a_file,
bool $overwrite =
false,
bool $a_flat =
false): bool
698 if (defined(
'DEVMODE') && DEVMODE) {
699 trigger_error(
'Deprecated method called: ' . __METHOD__, E_USER_DEPRECATED);
703 return $DIC->legacyArchives()->unzip(
717 $def_arr = explode(
",", SUFFIX_REPL_DEFAULT);
718 foreach ($def_arr as $def) {
719 self::rRenameSuffix($a_dir, trim($def),
"sec");
722 $def_arr = explode(
",", SUFFIX_REPL_ADDITIONAL);
723 foreach ($def_arr as $def) {
724 self::rRenameSuffix($a_dir, trim($def),
"sec");
734 public static function rRenameSuffix(
string $a_dir,
string $a_old_suffix,
string $a_new_suffix): bool
736 if ($a_dir ===
"/" || $a_dir ===
"" || is_int(strpos($a_dir,
".."))
737 || trim($a_old_suffix) ===
"") {
742 if (!@is_dir($a_dir)) {
747 $dir = opendir($a_dir);
749 while ($file = readdir($dir)) {
750 if ($file !==
"." && $file !==
"..") {
752 if ($file ===
'...') {
753 unlink($a_dir .
"/" . $file);
757 if (@is_dir($a_dir .
"/" . $file)) {
762 if (@is_file($a_dir .
"/" . $file)) {
764 if (strrpos($file,
'.') == (strlen($file) - 1)) {
766 rename($a_dir .
'/' . $file, substr($a_dir .
'/' . $file, 0, -1));
767 }
catch (Throwable) {
769 unlink($a_dir .
'/' . $file);
772 $file = substr($file, 0, -1);
775 $path_info = pathinfo($a_dir .
"/" . $file);
777 if (strtolower($path_info[
"extension"] ??
'') === strtolower($a_old_suffix)) {
778 $pos = strrpos($a_dir .
"/" . $file,
".");
779 $new_name = substr($a_dir .
"/" . $file, 0, $pos) .
"." . $a_new_suffix;
781 if (file_exists($new_name)) {
782 if (is_dir($new_name)) {
788 rename($a_dir .
"/" . $file, $new_name);
798 $path = preg_replace(
"/[\/\\\]+$/",
"",
$path);
799 return (
string)
$path;
807 $convertPhpIniSizeValueToBytes =
function ($phpIniSizeValue) {
808 if (is_numeric($phpIniSizeValue)) {
809 return $phpIniSizeValue;
812 $suffix = substr($phpIniSizeValue, -1);
813 $value = substr($phpIniSizeValue, 0, -1);
815 switch (strtoupper($suffix)) {
836 $uploadSizeLimitBytes = min(
837 $convertPhpIniSizeValueToBytes(ini_get(
'post_max_size')),
838 $convertPhpIniSizeValueToBytes(ini_get(
'upload_max_filesize'))
841 return $uploadSizeLimitBytes;
This class provides the data size with additional information to remove the work to calculate the siz...
Indicates that the directory is missing or not found.
Indicates that a file is missing or not found.
Indicates general problems with the input or output operations.
The legacy path helper provides convenient functions for the integration of the filesystem service wi...
Base class for ILIAS Exception handling.
Class ilFileServicesFilenameSanitizer.
Class ilFileServicesPolicy.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static utf8_encode(string $string)
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
static getWebspaceDir(string $mode="filesystem")
get webspace directory
static getASCIIFilename(string $a_filename)
static getDir(string $a_dir, bool $a_rec=false, ?string $a_sub_dir="")
get directory
static recursive_dirscan(string $dir, array &$arr)
Recursively scans a given directory and writes path and filename into referenced array.
static zip(string $a_dir, string $a_file, bool $compress_content=false)
static makeDir(string $a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
static ilTempnam(?string $a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
static rename(string $a_source, string $a_target)
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static unzip(string $a_file, bool $overwrite=false, bool $a_flat=false)
unzip file
static sanitateTargetPath(string $a_target)
static getDataDir()
get data directory (outside webspace)
static dirsize(string $directory)
get size of a directory or a file.
static renameExecutables(string $a_dir)
static getSafeFilename(string $a_initial_filename)
static createDirectory(string $a_dir, int $a_mod=0755)
create directory
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 _sanitizeFilemame(string $a_filename)
static getPhpUploadSizeLimitInBytes()
static removeTrailingPathSeparators(string $path)
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 rCopy(string $a_sdir, string $a_tdir, bool $preserveTimeAttributes=false)
Copies content of a directory $a_sdir recursively to a directory $a_tdir.
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")