note 60521 added to function.move-uploaded-file
| From: | mancow at macfilez dot net | Date: | Tue, 10 Jan 2006 05:31:48 +0000 |
| Subject: | note 60521 added to function.move-uploaded-file | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-101476@lists.php.net to get a copy of this message | ||
To nouncad at mayetlite dot com,
That function will work fine for files with a 3-character file extension. However, it is worth
noting that there are valid, registered file extensions that are longer than 3 characters. For
example, a JPEG file can be denoted by *.jpg (and others), but it can also have *.jpeg as a valid
extension. Check out http://www.filext.com/ for a good
reference of file extensions.
The best bet to me would be parsing the uploaded file's name
($_FILES['uploadedfile']['name']) based on the presence of dots. Another wrench
in the gears: a file can have dots in the filename. That's easy enough to handle -- just
explode() the file name and hope that the last element in the array it gives you is the file
extension (you can always validate it if you're so inclined). Then just piece it together in a
string accordingly by stepping through the array (don't forget to add those dots back to where
they were!), appending a guaranteed unique string of characters (or enumerate it like you were
doing, keeping track via a loop), and finally tacking on the file extension.
You may have other mechanisms for verifying a file's extension, such as a preg_match on the
whole name, using something like "/\\.(gif|jpg|jpeg|png|bmp)$/i" (more can, of course, be
added if you so desire) for the most common types of images found on the web.
For blindly guaranteeing an uploaded file will be uniquely named, this seems like a fantastic way to
go. Enjoy!
----
Manual Page -- http://www.php.net/manual/en/function.move-uploaded-file.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+60521
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60521&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60521&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60521&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60521&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60521&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60521&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60521&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+60521&report=yes
Search -- http://master.php.net/manage/user-notes.php