this causes errors in file sharing (share with url and password)

Try this:
OC_Files::get($dir, array($file), $_SERVER['REQUEST_METHOD'] == 'HEAD' ? true : false);