Added a useful function in moodlelib to resolve filename collisions when moving files around. Perhaps useful for backup or importing or files/index.php

This commit is contained in:
mjollnir_ 2006-03-06 00:36:06 +00:00
parent 988f9ddff0
commit 74ffb569c6

View File

@ -4247,6 +4247,43 @@ function print_file_upload_error($filearray = '', $returnerror = false) {
}
/**
* handy function to loop through an array of files and resolve any filename conflicts
* both in the array of filenames and for what is already on disk.
* not really compatible with the similar function in uploadlib.php
* but this could be used for files/index.php for moving files around.
*/
function resolve_filename_collisions($destination,$files,$format='%s_%d.%s') {
foreach ($files as $k => $f) {
if (check_potential_filename($destination,$f,$files)) {
$bits = explode('.', $f);
for ($i = 1; true; $i++) {
$try = sprintf($format, $bits[0], $i, $bits[1]);
if (!check_potential_filename($destination,$try,$files)) {
$files[$k] = $try;
break;
}
}
}
}
return $files;
}
/**
* @used by resolve_filename_collisions
*/
function check_potential_filename($destination,$filename,$files) {
if (file_exists($destination.'/'.$filename)) {
return true;
}
if (count(array_keys($files,$filename)) > 1) {
return true;
}
return false;
}
/**
* Returns an array with all the filenames in
* all subdirectories, relative to the given rootdir.