note 77674 added to function.move-uploaded-file
| From: | dev at asp55-ns dot com | Date: | Fri, 07 Sep 2007 19:06:51 +0000 |
| Subject: | note 77674 added to function.move-uploaded-file | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-130300@lists.php.net to get a copy of this message | ||
So yet another (though I believe simpler and more fool proof) way of ensuring the file uploaded has
a unique name is to use a for loop to test the existence of the file, and increment the n value to
append to the file name.
As for the issue of ensuring n comes before the file extension, I suggest using strrpos to find the
position of the last dot, which presumably should come before the extension, and then use 2
instances of substr to split it up.
$savefile= $uploaddir . basename($_FILES['userfile']['name']);
$uploadfile = $savefile; //duplicate variable so that appending numbers doesn't result in
sequences like 0 01 012 and so forth.
for($n=0;file_exists($uploadfile);$n++) {
$dotPos = strrpos($savefile,".");
$savefile = substr($savefile,0,$dotPos).$n.substr($savefile,$dotPos);
}
----
Server IP: 64.71.164.2
Probable Submitter: 63.164.145.198
----
Manual Page -- http://www.php.net/manual/en/function.move-uploaded-file.php
Edit -- https://master.php.net/note/edit/77674
Del: integrated -- https://master.php.net/note/delete/77674/integrated
Del: useless -- https://master.php.net/note/delete/77674/useless
Del: bad code -- https://master.php.net/note/delete/77674/bad+code
Del: spam -- https://master.php.net/note/delete/77674/spam
Del: non-english -- https://master.php.net/note/delete/77674/non-english
Del: in docs -- https://master.php.net/note/delete/77674/in+docs
Del: other reasons-- https://master.php.net/note/delete/77674
Reject -- https://master.php.net/note/reject/77674
Search -- https://master.php.net/manage/user-notes.php