geroid Posted May 25, 2009 Share Posted May 25, 2009 Hi I'm starting work on a file upload section to a website. I've never done uploading script before and have found a small script that works. Now I want to pull the script apart and understand how it works and what it is doing. Can you please explain exactly what this script is doing line for line: function getExtension($str) { //Scan the string to find the last postion of the dot(.) in the string - where's the dot? $i = strrpos($str,"."); //If the dot is found if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } Also, I'm a little worried that I might exclude something important in an upload script, Does anyone know a good tutorial for this. The dos and don'ts. What should I be doind to make sure it's a good solid script. The goal is simply to allow an administrator to upload a picture to a folder and put those details in a database table. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/159548-help-with-understanding-this-function/ Share on other sites More sharing options...
Dathremar Posted May 25, 2009 Share Posted May 25, 2009 function getExtension($str) { $i = strrpos($str,"."); // Position of the "." in the string, if there is one if (!$i) { return ""; } $l = strlen($str) - $i; // The length of the string subtract the position of the "." $ext = substr($str,$i+1,$l); // gets the extension return $ext; } But to be honest I would just use explode function. Something like this: function getExtension($str) { $parts_string = explode( ".", $str ) if (count($parts_string) > 1) return $parts_string[1]; else return ""; } Quote Link to comment https://forums.phpfreaks.com/topic/159548-help-with-understanding-this-function/#findComment-841572 Share on other sites More sharing options...
Daniel0 Posted May 25, 2009 Share Posted May 25, 2009 $i = strrpos($str,"."); This searches backwards in $str for a dot and stores the position in $i (the first character has position 0). See: strrpos. if (!$i) { return ""; } strrpos returns FALSE if no match is found, so if it's not the case that $i evaluates to true (i.e. it's false), an empty string is returned. $l = strlen($str) - $i; Here the the length of $str is found, and $i is subtracted, or in other words, $l now contains the length of the extension. See: strlen. $ext = substr($str,$i+1,$l); substr returns a substring, i.e. part of a string. The first argument is the string, the second is the starting position, and the third is the number of bytes to get (or the length if you wish). In this case the third argument (and thus the statement above this line) is irrelevant. File extensions are always on the end of the filename and substr goes until the end of the string by default. The result is stored in a variable called $ext. This could have been substr($str,$i+1). return $ext; The extension is returned. There is a function called pathinfo though, so you could just have done this: $ext = pathinfo($str, PATHINFO_EXTENSION); But to be honest I would just use explode function. Something like this: That won't work. What if it's called foo.bar.txt. Your function will return bar, but the extension is txt. Quote Link to comment https://forums.phpfreaks.com/topic/159548-help-with-understanding-this-function/#findComment-841573 Share on other sites More sharing options...
geroid Posted May 25, 2009 Author Share Posted May 25, 2009 Thanks for that explanation. As I say I'm reasonably new to this upload script thing. Can I just ask again if you know any good tutorial or script example that I could see. As I say, the goal is to allow an image to be uploaded to a folder. I'd love a script example to see how a good one should be written. I'm worried about not including something important. Quote Link to comment https://forums.phpfreaks.com/topic/159548-help-with-understanding-this-function/#findComment-841579 Share on other sites More sharing options...
ToonMariner Posted May 25, 2009 Share Posted May 25, 2009 Dangerous to assume only one . in the file name.... object.class.php is fairly common out there so lets just get the extension... function getExt($file) { return substr($file,(strrchr($file,'.')+1)); } beaten by the kettle Quote Link to comment https://forums.phpfreaks.com/topic/159548-help-with-understanding-this-function/#findComment-841581 Share on other sites More sharing options...
Daniel0 Posted May 25, 2009 Share Posted May 25, 2009 But to be honest I would just use explode function. Something like this: Also, the explode() method is more expensive. Consider how you would implement explode() and strrpos() yourself. function myStrrpos($haystack, $needle, $offset = 0) { for ($i = strlen($haystack) - 1 - $offset; $i >= 0; $i--) { if ($haystack[$i] == $needle) { return $i; } } return false; } function myExplode($delimiter, $string, $limit = null) { $parts = array(); for ($i = 0, $j = 0, $length = strlen($string); $i < $length; $i++) { if ($string[$i] == $delimiter && ($limit === null || $limit > $j+1)) { $j++; continue; } if (!isset($parts[$j])) { $parts[$j] = $string[$i]; } else { $parts[$j] .= $string[$i]; } } return $parts; } It takes up more memory processing power to explode a string into parts by a delimiter than simply searching backwards for a particular character. Quote Link to comment https://forums.phpfreaks.com/topic/159548-help-with-understanding-this-function/#findComment-841587 Share on other sites More sharing options...
geroid Posted May 25, 2009 Author Share Posted May 25, 2009 Hi toonmariner You just wrote "Dangerous to assume only one . in the file name...." Is my function adequate for the job or should I amend it? function getExtension($str) { $i = strrpos($str,"."); if (!$_SESSION['i']) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; Quote Link to comment https://forums.phpfreaks.com/topic/159548-help-with-understanding-this-function/#findComment-841592 Share on other sites More sharing options...
Daniel0 Posted May 25, 2009 Share Posted May 25, 2009 No it is not (except you're misusing sessions), but as previously mentioned, why not use the built-in pathinfo when it's there? Quote Link to comment https://forums.phpfreaks.com/topic/159548-help-with-understanding-this-function/#findComment-841593 Share on other sites More sharing options...
geroid Posted May 25, 2009 Author Share Posted May 25, 2009 Sorry Toonmariner, this is the function here. I was just experimenting and using sessions to echo the contents to see them. function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } I'm not sure whether you think this function is good enough or not. Could you amend it with the pathinfo function for me to try. Quote Link to comment https://forums.phpfreaks.com/topic/159548-help-with-understanding-this-function/#findComment-841599 Share on other sites More sharing options...
Daniel0 Posted May 25, 2009 Share Posted May 25, 2009 Amend it? You just do like this: There is a function called pathinfo though, so you could just have done this: $ext = pathinfo($str, PATHINFO_EXTENSION); That is literally the only thing you need to do... There is no need to write your own function. Quote Link to comment https://forums.phpfreaks.com/topic/159548-help-with-understanding-this-function/#findComment-841600 Share on other sites More sharing options...
geroid Posted May 25, 2009 Author Share Posted May 25, 2009 Daniel0 I'm new to this so I hope I don't sound too stupid but does the rest of the function stay the same. That is, do I just add your line in like this: function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = pathinfo($str, PATHINFO_EXTENSION); return $ext; } And finaly, is this funtion good now for the job? Quote Link to comment https://forums.phpfreaks.com/topic/159548-help-with-understanding-this-function/#findComment-841601 Share on other sites More sharing options...
wildteen88 Posted May 25, 2009 Share Posted May 25, 2009 This is all your function needs to be function getExtension($str) { $ext = pathinfo($str, PATHINFO_EXTENSION); return $ext; } Quote Link to comment https://forums.phpfreaks.com/topic/159548-help-with-understanding-this-function/#findComment-841602 Share on other sites More sharing options...
geroid Posted May 25, 2009 Author Share Posted May 25, 2009 Thanks all That's much better. I like that function. Much appreciated. I'll just go and read up on it now. Quote Link to comment https://forums.phpfreaks.com/topic/159548-help-with-understanding-this-function/#findComment-841608 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.