/** * Moves a file or directory to a new location * * <code> * \bbn\file\dir::move("C:\Documents\Test\Old", "C:\Documents\Test\New"); * </code> * * @param string $orig The file to be moved * @param string $dest The full name of the destination (including basename) * @param mixed $st If $st === true it will be copied over if the destination already exists, otherwise $st will be used to rename the new file in case of conflict * @param int $length The number of characters to use for the revision number; will be zerofilled * * @return string the (new or not) name of the destination or false */ public static function move($orig, $dest, $st = '_v', $length = 0) { if (file_exists($orig) && self::create_path(dirname($dest))) { if (file_exists($dest)) { if ($st === true) { self::delete($dest); } else { $i = 1; while ($i) { $dir = dirname($dest) . '/'; $file_name = \bbn\str\text::file_ext($dest, 1); $file = $file_name[0] . $st; if ($length > 0) { $len = strlen(\bbn\str\text::cast($i)); if ($len > $length) { return false; } $file .= str_repeat('0', $length - $len); } $file .= \bbn\str\text::cast($i); if (!empty($file_name[1])) { $file .= '.' . $file_name[1]; } $i++; if (!file_exists($dir . $file)) { $dest = $dir . $file; $i = false; } } } } if (rename($orig, $dest)) { return basename($dest); } } return false; }
/** * Converts a numeric array to an associative one, using the values alternatively as key or value. * * <code> * \bbn\tools::to_keypair(['Test', 'TestFile', 'Example', 'ExampleFile']); //Returns ['Test' => 'TestFile', 'Example' => 'ExampleFile'] * </code> * * @param array $arr must contain an even number of values. * @param bool $protected if false no index protection will be performed, default: "1". * * @return array|false */ public static function to_keypair($arr, $protected = 1) { $num = count($arr); $res = []; if ($num % 2 === 0) { $i = 0; while (isset($arr[$i])) { if (!is_string($arr[$i]) || !$protected && !preg_match('/[0-9A-z\\-_]+/8', \bbn\str\text::cast($arr[$i]))) { return false; } $res[$arr[$i]] = $arr[$i + 1]; $i += 2; } } return $res; }