The following code works on my laptop but doesn\'t work on remote server:
I think you might have problems with filepath. Check if pathinfo returns valid extension and add after $filename declaration something to check if it exists and is readable
if(!is_file($file) || !is_readable($file)){ die('Readable file not found');}