Jump to content

Help with understanding this function


geroid

Recommended Posts

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

Link to comment
Share on other sites

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 ""; 
}

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ;)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.