Я хочу загрузить файлы с удаленного сервера и сохранить эти файлы на моем сервере именно так, как этот ответ здесь
https://stackoverflow.com/a/48141751/9625566
Я использую его код, нопроблема заключается не в сохранении файла с исходным именем файла удаленного сервера, а в сохранении файла как "6897823" , но я хочу, чтобы имя файла было точно таким же, как и на удаленном сервере,
"fantastic.beasts.and.where.to.find.them. (2016) .eng.1cd. (6897823) .zip"
Это пример ссылки
https://dl.opensubtitles.org/en/download/src-api/vrf-f58b0bc4/sub/6897823
Я пробовал CURL & file_get_content()
, для обоих я должен указать имя файла по умолчанию, они не получают имя файла по ссылке,
<?php
$url = "https://dl.opensubtitles.org/en/download/src-api/vrf-f58b0bc4/sub/6897823";
class Downloader
{
private $path;
public $filename;
public function __construct()
{
$this->path = dirname(__FILE__);
}
public function getFile($url)
{
$fileName = $this->getFileName($url);
if(!file_exists($this->path."/".$fileName)){
$this->downloadFile($url,$fileName);
}
return file_get_contents($this->path."/".$fileName);
}
public function getFileName($url)
{
$slugs = explode("/", $url);
$this->filename = $slugs[count($slugs)-1];
return $this->filename;
}
private function downloadFile($url,$fileName)
{
//This is the file where we save the information
$fp = fopen ($this->path.'/'.$fileName, 'w+');
//Here is the file we are downloading, replace spaces with %20
$ch = curl_init(str_replace(" ","%20",$url));
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
// write curl response to file
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// get curl response
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
}
$downloader = new Downloader();
$content = $downloader->getFile($url);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$downloader->filename.'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . strlen($content));
print $content;
exit;
?>
Я ожидаю, что имя файла будет
"fantastic.beasts.and.where.to.find.them. (2016) .eng.1cd. (6897823) .zip"
Но на выходе получается 6897823
Спасибо за чтение.