note 20218 added to features.file-upload

From: Date: Tue, 26 Mar 2002 19:57:54 +0000
Subject: note 20218 added to features.file-upload
Groups: php.notes 
Request: Send a blank email to php-notes+get-28245@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); -- http://www.php.net/manual/en/features.file-upload.php http://master.php.net/manage/user-notes.php?action=edit+20218 http://master.php.net/manage/user-notes.php?action=delete+20218 http://master.php.net/manage/user-notes.php?action=reject+20218

« previous php.notes (#28245) next »