1

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.

Anriëtte Myburgh
  • 13,347
  • 11
  • 51
  • 72
  • what's the value of $filename and $dir you are inputting? – Natrium Jan 21 '10 at 10:19
  • I only have one `return` statement, because it is 'n recursive function, when I echo `$filename` right before the `return`, the string is 100% correct, but the return does not return the value. – Anriëtte Myburgh Jan 21 '10 at 10:22
  • 1
    The problem is that `return` is only executed it `is_file()` is false; if it's true there is no `return` statement to be executed. – Matteo Riva Jan 21 '10 at 10:25
  • 1
    Even in a recursive function, the result of the inner call needs to be `return`ed to the outer call – Gareth Jan 21 '10 at 10:26

4 Answers4

12

The line:

getNewFileName($filename, $dir);

needs a return:

return getNewFileName($filename, $dir);
Ben James
  • 121,135
  • 26
  • 193
  • 155
1

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
  • 161,348
  • 33
  • 346
  • 320
0

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
  • 616,129
  • 168
  • 910
  • 942
-1
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 Müller
  • 1,433
  • 11
  • 24
  • 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 Jan 21 '10 at 10:25