note 20218 rejected from features.file-upload by victor
| From: | victor@php.net | Date: | Sat, 24 Aug 2002 15:42:31 +0000 |
| Subject: | note 20218 rejected from features.file-upload by victor | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-35513@lists.php.net to get a copy of this message | ||
I found cwq@detnet.com's post on not overwriting an existing file by appending a number to it
very helpful. The code unfortunately will not work correctly once you get to filename10.foo. Here
is my rewrite of the code to fix this, as well as allowing "." in the directory path
structure. Maybe someone will find a bug in mine and/or make it more efficient?
define("c_FileUploadPath", "images/profile");
...
$FileName = $_FILES['userfile']['name'];
while (file_exists(c_FileUploadPath.$FileName)) {
$SeperatedFileName = explode(".",$FileName);
$FileNameNumIndex = 0;
$FileNameNum = 0;
while (is_numeric(substr($SeperatedFileName[0],-($FileNameNumIndex+1))))
$FileNameNumIndex++;
if ($FileNameNumIndex != 0) {
$FileNameNum = substr($SeperatedFileName[0],-$FileNameNumIndex);
$SeperatedFileName[0] = substr($SeperatedFileName[0], 0,
(strlen($SeperatedFileName[0])-(strlen(strval($FileNameNum)))));
}
$NextFileNameNum = $FileNameNum + 1;
$SeperatedFileName[0] = $SeperatedFileName[0] . "$NextFileNameNum";
$FileName = implode(".",$SeperatedFileName);
}
copy($_FILES['userfile']['tmp_name'], c_FileUploadPath . $FileName);