by Davo Smith.
Have you tried looking at what line 3935 of lib/filelib.php is doing?
That is likely to be the best place to look for errors.
Put some 'echo' or 'var_dump' statements in there to output the variables (or even better, step through with xdebug) and then see what happens if you access the URL for the file directly.