note 93658 deleted from function.move-uploaded-file by danbrown
| From: | danbrown@php.net | Date: | Tue, 22 Sep 2009 14:42:51 +0000 |
| Subject: | note 93658 deleted from function.move-uploaded-file by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-160935@lists.php.net to get a copy of this message | ||
Note Submitter: mitxela
----
Using the function Zarel posted, if the user downloads and uploads a file repeatedly the filename
will look something like filename[1][1][1][1].ext
This function renames them with a space and round brackets around the number, the same as renaming
files on Windows. If the number is already there, it will increment it instead of adding another
pair of brackets.
You could quite easily change it to have square brackets and no space if you like.
<?php
function filename_increment($fname) {
//Check there is an extension
if (strstr($fname, '.')) {
$ext=substr($fname,strrpos($fname,'.'));
$filename=substr($fname,0,strrpos($fname,'.'));
} else $filename=$fname;
//Increment the number, or add (2) onto it if there is no number
$filename= ((preg_match("/( \([0-9]+\))$/",$filename)) ? preg_replace("/(
\(([0-9]+)\))$/e","' ('.('$2'+1).')'",$filename) :
$filename." (2)" );
return $filename.$ext;
}
//Can be used something like this:
while (file_exists($dir.$filename)) {
$filename=filename_increment($filename);
}
?>