At 5/4/09 12:02 PM, DFox wrote:
At 5/4/09 10:49 AM, CronoMan wrote:
Extract the 4 last characters of the filename.
But why? What if the extension is 1 character? 2 characters? 3 characters? 4 characters? Then you have a messed up script. So no, that logic wouldn't work. Rather, like other people have suggested in this thread, you would use explode() to get the last part, which has to be the extension, and it doesn't matter how many characters long it is.
I'm going on the assumption that he wanted to check if it were a .zip file. That's 4 characters
If there were less than 4 characters, strcmp would return a negative number, and it wouldn't be a .zip-file
If you want a way to retrieve the extension of any file, you create a function for that
The explode-solution is not viable when there is an easier, and less resource intensive way to do it. Exploding into an array so you can basically just feed the garbage collector with useless junk is a waste, no matter how small and unsignificant it might seem. There's no reason to jump through burning hoops just in order to retrieve the extension of a file
if you instead wrote a function which would retrieve the last occurance of . and then return the portion after that would be alot simpler and more resource friendly
So instead of yelling at PHP, learn how to write working, adaptable algorithms first because from this example it doesn't really seem like PHP is the problem.
I'm not yelling at PHP, you pointed out an obvious inconsistency in PHP - I mistook strcmp to have the same construct as strpos and strstr. Which it doesn't
My "outline" does exactly what the purpose is - it checks to see if the filename ends with .zip, which means it's a zip-file.
I personally don't think it's such a bad idea to assume that a file is a zip-file, if it ends with the string ".zip" - I mean, that's what extensions are for, right?
No, it isn't. When you're working with files like exe's, zips, etc, that can actually contain executable/dangerous code, I think you want to know if the file is what the extension says it is. When you're working with jpg's, gifs, pngs, etc, it doesn't really matter if the file integrity isn't there. What's the worst that can happen? An invalid image error?
Web server's shouldn't try to execute code, nor do I think they can (except CGI of course)
So just checking the extension at first is more than enough - keep it simple
At 5/4/09 03:02 PM, DFox wrote:
At 5/4/09 02:47 PM, elbekko wrote:
Actually:
http://www.h-online.com/security/Risky-M IME-sniffing-in-Internet-Explorer--/feat ures/112589
OK, so I guess you want to check the integrity of image files also, although I'm not sure the easiest way to do that. Do you know of any classes for this?
http://no2.php.net/manual/en/book.zip.ph p