 <p>Is the user supplied name important? </p> <p>If it is not, one technique i like to do to normalize file names in that case is to simply hash them with something like sha1 or even md5. Then add your timestamp, and ids or what not to that, this takes care of a lot of issues with special characters such as ".", "\" and "/" ( dot dot dash and directory traversal ) in the file names.</p> <p>@hwnd's regular expression should do the trick, but I though I would throw this out there.</p> <p>so with hashing you'd get cleaner ( but less meaningful ) names like this </p> <pre><code>da39a3ee5e6b4b0d3255bfef95601890afd80709.jpg </code></pre> <p>then you can add your unique numbers on</p> <pre><code>da39a3ee5e6b4b0d3255bfef95601890afd80709-1234568764564558.jpg </code></pre> <p>you could even salt the filename with the timestamp first and then hash them to get filenames all 40 characters long, and the chance of hash collisions is very minimal unless your dealing with 10's of thousands of files, in which case just up the hashing to sha256 etc.</p>