note 19830 deleted from features.file-upload by philip
| From: | philip@php.net | Date: | Sat, 15 Feb 2003 15:32:29 +0000 |
| Subject: | note 19830 deleted from features.file-upload by philip | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-43990@lists.php.net to get a copy of this message | ||
I don't know if anybody else besides me will find this useful, but I just wrote this bit to
handle files that were uploaded to my server, but to not allow them to overwrite an existing file
with the same name. This will append the number 1 to the end of the file name and if THAT file
exists, it will increment the number until it finds an unused one.
One thing this wouldn't handle, is if you have periods in your directory name for some reason.
Replace "images/profile" with your directory path.
$glued_filename = "images/profile/" . $_FILES['userfile']['name'];
$i = 1;
while (file_exists($glued_filename)) {
$separated_filename = explode(".",$glued_filename);
if (substr($separated_filename[0],-1) == $i) {
$separated_filename[0] = substr($separated_filename[0], 0,
(strlen($separated_filename[0])-1));
$i++;
}
$separated_filename[0] = $separated_filename[0] . "$i";
$glued_filename = implode(".",$separated_filename);
}
copy($_FILES['userfile']['tmp_name'], $glued_filename);