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.