note 77674 deleted from function.move-uploaded-file by nlopess

From: Date: Fri, 21 Sep 2007 13:39:16 +0000
Subject: note 77674 deleted from function.move-uploaded-file by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-130664@lists.php.net to get a copy of this message
Note Submitter: dev at asp55-ns dot com ---- 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); }

« previous php.notes (#130664) next »