I'd guess that $local is rejected because it's a relative filename (at least, it will be if it matches your $filename regex).
Can't find any hard data to support my guess at the moment, though. Try prepending a fixed directory to the $local path, i.e. do this before calling the method: