To deal with this situation, some of the practice is to use a timestamp to rename the new file, there are a number of sequential increments, in order to solve the troublesome operation, I recently wrote a frename function, you can flexibly according to the custom rules to obtain the new name of the uploaded file, hereby share.
Copy the Code code as follows:
function Frename ($file, $rule = ' {timestamp} ', $force = True) {
/* -----------------------
* AUTHOR:M35
* DATE:2009/8/11
* A new name for the file according to the naming convention, to deal with the file name already existed when uploading files and other operations
* @parm1 $file--File physical path
* @parm2 $rule--naming rules, default timestamp
* @parm3 $force--whether it is mandatory, if it is mandatory, even if the target file does not exist will be named, the default enforcement
* Retrun STR--New file path named according to $rule
Example
Echo ' Default rule (timestamp): '. Frename (__file__);
Echo '
Ordinal increment rule: '. Frename (__file__, ' {name}{n} ');
Echo '
Ordinal increment rule for n leading 0: '. Frename (__file__, ' {name}{5n} ');
Echo '
With separators_An ordinal increment rule with 3 leading 0: '. Frename (__file__, ' {3n}<_>{name} ');
Echo '
Rules that use the combination of time-date elements: '. Frename (__file__, ' {y}_{m}_{d}_{h}_{i}_{s} ');
Echo '
Use the shorthand form of the time-date element combination rule: '. Frename (__file__, ' {y}_{m}_{d}_{h}_{i}_{s} ');
Echo '
Other custom combinations: '. Frename (__file__, ' {timestamp}_{name}<_>{n} ');
----------------------- */
if (! $force &&!file_exists ($file)) return $file;
$filename = basename ($file);
$path = Str_replace ($filename, ", $file);
$suffix = substr ($filename, Strrpos ($filename, '. '));
$name = Str_replace ($suffix, ", $filename);
$timestamp = time ();
List ($y, $Y, $m, $M, $d, $D, $h, $H, $i, $s) = explode (', ', date (' y,y,m,n,d,j,h,g,i,s '));
$tempname = Str_replace (
Array (' {name} ', ' {timestamp} ', ' {y} ', ' {y} ', ' {m} ', ' {m} ', ' {d} ', ' {d} ', ' {h} ', ' {h} ', ' {i} ', ' {s} '),
Array ($name, $timestamp, $y, $Y, $m, $M, $d, $D, $h, $H, $i, $s),
$rule
);
if (Preg_match ('/\{(\d?) n\}/', $rule, $n)) {
Preg_match ('/< ([^>]+) >/', $tempname, $sep);
$file = $path. Str_replace (Array ($n [0], $sep [0]), Array (","), $tempname). $suffix;
if (!file_exists ($file)) return $file;
$tempname = Str_replace ($sep [0], $sep [1], $tempname);
$tname = $tempname;
$i = 1;
do {
$nn = sprintf ("%0{$n [1]}s", $i);
$tempname = Str_replace ($n [0], $nn, $tname);
$file = $path. $tempname. $suffix;
}
while (File_exists ($file));
return $file;
} else {
$file = $path. $tempname. $suffix;
if (file_exists ($file)) return false;
else return $path. $tempname. $suffix;
}
}
Echo ' Default rule (timestamp): '. Frename (__file__);
Echo '
Ordinal increment rule: '. Frename (__file__, ' {name}{n} ');
Echo '
Ordinal increment rule for n leading 0: '. Frename (__file__, ' {name}{5n} ');
Echo '
With separators_An ordinal increment rule with 3 leading 0: '. Frename (__file__, ' {3n}<_>{name} ');
Echo '
Rules that use the combination of time-date elements: '. Frename (__file__, ' {y}_{m}_{d}_{h}_{i}_{s} ');
Echo '
Use the shorthand form of the time-date element combination rule: '. Frename (__file__, ' {y}_{m}_{d}_{h}_{i}_{s} ');
Echo '
Other custom combinations: '. Frename (__file__, ' {timestamp}_{name}<_>{n} ');
?>
The above describes the frename php flexible file naming function Frename, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.