Functions
WebDev/*nix assorted functions & hacks
Search
find all
Array
-
Bash
-
Classes
-
Config
-
DateTime
-
Debug
-
dotjs
-
File
-
foooo
-
Git
-
HTML
-
Images
-
Javascript
-
Linux
-
Math
-
Misc
-
MySQL
-
OS X
-
Reading
-
Redis
-
Shell scripts
-
snippets
-
SQL
-
Strings
-
TextMate
-
Web
-
\(^o^)/ Fun!
-
_Misc hacks
-
_Pages
File
clean_filename($filename)
duplicate_name($name, etc)
fetch_dirlist($path='', $exts='')
file_ext($file)
file_label($file, $label)
fsize($x)
resize_image($image, $sizes)
7
found
auth
Name:
Category: File
Body:
# Creates and returns new filename based on given name - Doesn't actually duplicate any files. # Behaviour sto^Wborrowed from OS X (sort of - apparently OS X also makes use of a list of known extensions). # $list = array of filenames in the target directory (if none given, it will still return a new name) # $max = max length of filename #____________________________________________________ # duplicate_name($orig, $list = array(), $max = 64) / function duplicate_name($orig, $list = array(), $max = 64) { $ext = ''; $counter = 0; $list = (array) $list; $max = (int) $max; $newname = $orig; do { $name = $newname; # name in, newname out if (preg_match('/ copy$| copy \d+$/', $name, $matches)) { // don't even check for extension, name ends with " copy[ digits]" # preg hereunder matches anything with at least one period in the middle and an extension of 1-5 characters }elseif (preg_match('/(.+)\.([^.]{1,5})$/', $name, $parts)) { # split to name & extension list($name, $ext) = array($parts[1], $parts[2]); } if (preg_match('/ copy (\d+)$/', $name, $digits)) { $newname = substr($name, 0, - strlen($digits[1])) . ($digits[1] + 1); $cutlen = 6 + strlen($digits[1]+1); // ' copy ' + digits }elseif(preg_match('/ copy$/', $name, $digits)) { $newname = $name . ' 1'; $cutlen = 7; // ' copy' + ' 1' }else{ $newname = $name . ' copy'; $cutlen = 5; // ' copy' } if ($ext) { $newname .= '.' . $ext; $cutlen += strlen($ext) + 1; } if ($max > 0) { if (strlen($newname) > $max) { $newname = substr($newname, 0, max($max - $cutlen, 0)) . substr($newname, -$cutlen); if (strlen($newname) > $max) {echo "duplicate_name() error: Can't keep the new name under given max length.\n"; return false;} } } if ($counter++ > 500) {echo "duplicate_name() error: Too many similarly named files or infinite while loop.\n"; return false;} } while (in_array($newname, $list)); return $newname; }
Footer