Вот решение без скручивания, но для этого должен быть включен url_fopen.
$response_headers = get_headers($url,1);
// first take filename from url
$filename = basename($url);
// if Content-Disposition is present and file name is found use this
if(isset($response_headers["Content-Disposition"]))
{
// this catches filenames between Quotes
if(preg_match('/.*filename=[\'\"]([^\'\"]+)/', $response_headers["Content-Disposition"], $matches))
{ $filename = $matches[1]; }
// if filename is not quoted, we take all until the next space
else if(preg_match("/.*filename=([^ ]+)/", $response_headers["Content-Disposition"], $matches))
{ $filename = $matches[1]; }
}
// if no Content-Disposition is found use the filename from url
// before using the filename remove all unwanted chars wich are not on a-z e.g. (I the most chars which can be used in filenames, if you like to renove more signs remove them from the 1. parameter in preg_replace
$filename = preg_replace("/[^a-zA-Z0-9_#\(\)\[\]\.+-=]/", "",$filename);
// at last download / copy the content
copy($url, $filename);
ОБНОВЛЕНИЕ: Имена файлов в Content-Disposition могут иметь пробелы (в этом случае они пишутся в одинарных или двойных кавычках). Я решил этот случай со вторым блоком preg_match.