What I'd like to do is instead of reporting an error saying the file name already exists, choose a different name for the file that's available. How do I assign a unique name to the file (for example, filename2.jpg, filename3.jpg)? I can do it using what I know about do while loops, but I'm not sure if there's a particularly simple or built-in way.
The answer may depend on whether or not you need the actual file name to me semantically meaningful. If not, you could just use uniqid() to create a unique file name, e.g.:
PHP Code:
$fileName = uniqid(true).'.jpg';
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
I believe that the $_FILES['field_name']['tmp_name'] value is similarly constructed via a uniqid-like method, so you could probably just use that (filtered through basename() to strip off the directory portion of the path).
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Bookmarks