views:

352

answers:

4

Hi there

I have constructed a function that takes a filename, and increments a counter in the filename and returns it, however, everything is correct, except the return does not return the filename.

Any help, please?

My code:

$filename = join("", array_reverse($date));
$filename .= ".xml";
$dir = "../gigs";
$file = $dir."/".$filename;

function getNewFileName($filename, $dir) {
if (is_file("$dir/$filename")) {
    if (strpos($filename, "_") === false) {
        $filename = str_replace(".xml","_1.xml",$filename);
        getNewFileName($filename, $dir);
    }
    else {
            $pos = strpos($filename, "_");
            $counter = (int)substr($filename, $pos+1,1);
            $counter++;
            $filename = substr($filename,0, $pos)."_".$counter.".xml";
            getNewFileName($filename, $dir);
        }
    } else {
                // echoing HERE shows that the string is manipulated correctly
        return (string)$filename; // but returning here is not working
    }
}

echo getNewFileName($filename, $dir); // <- this last line prints nothing out

Thanks in advance.

+1  A: 

This is what your function should look like:

function getNewFileName($filename, $dir) {
   if (is_file("$dir/$filename")) {
       if (strpos($filename, "_") === false) {
           $filename = str_replace(".xml","_1.xml",$filename);
           return getNewFileName($filename, $dir);
       }
       else {
               $pos = strpos($filename, "_");
               $counter = (int)substr($filename, $pos+1,1);
               $counter++;
               $filename = substr($filename,0, $pos)."_".$counter.".xml";
               return getNewFileName($filename, $dir);
       }
    }
    return (string)$filename;
}

echo getNewFileName($filename, $dir); // <- this last line prints nothing out
Jacob Relkin
A: 

Firstly, please try and format your code so the indents are readable. Second, you're just not returning from recursive calls to getNewFileName():

function getNewFileName($filename, $dir) {
  if (is_file("$dir/$filename")) {
    if (strpos($filename, "_") === false) {
      $filename = str_replace(".xml","_1.xml",$filename);
      return getNewFileName($filename, $dir); // here
    } else {
      $pos = strpos($filename, "_");
      $counter = (int)substr($filename, $pos+1,1);
      $counter++;
      $filename = substr($filename,0, $pos)."_".$counter.".xml";
      return getNewFileName($filename, $dir); // and here
    }
  } else {
    return (string)$filename;
  }
}

assuming that's your intent.

cletus
A: 
function getNewFileName($filename, $dir) {
  if (is_file("$dir/$filename")) {
    if (strpos($filename, "_") === false) {
      $filename = str_replace(".xml","_1.xml",$filename);
      return (string)$filename;
    } else {
      $pos = strpos($filename, "_");
      $counter = (int)substr($filename, $pos+1,1);
      $counter++;
      $filename = substr($filename,0, $pos)."_".$counter.".xml";
      return (string)$filename;
    }
  } else {
    return (string)$filename;
  }
}

Your function had a loop to infinity.

Thomas
It didn't loop to infinity, because the inner function call was using changing the arguments before passing them to the outer function call
Gareth
You're right, sorry.
Thomas
+6  A: 

The line:

getNewFileName($filename, $dir);

needs a return:

return getNewFileName($filename, $dir);
Ben James
In fact, both of the occurrences of that line do.
Gareth
Thank you very much! I don't understand really why that works, do you care to explain it to me?
Anriëtte Combrink
You're making a recursive function call. If you don't return it's return value down the stack, the base case will never evaluate true.
nikc