Строки в одинарных кавычках не анализируются для переменных, поэтому $_SESSION['$serverURL']
, вероятно, не будет работать так, как вы ожидаете. Я подозреваю, что вы имеете в виду $_SESSION[$serverURL]
или $_SESSION['serverURL']
.
Также вызов filesize()
, а затем readfile()
, вероятно, приведет к тому, что ваш скрипт выполнит два HTTP-запроса для извлечения файла с другого сервера (если это не будет каким-либо образом кэшировано). Вы можете сделать это в одном HTTP-запросе, используя cURL, что может быть лучшим вариантом. Вот краткий пример, вы должны иметь возможность адаптировать его, чтобы делать то, что вы хотите. Вы также можете рассмотреть возможность пересылки других заголовков, таких как заголовок Content-Type, с другого сервера (если они надежны), а не их повторную генерацию.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com');
//set callbacks to receive headers and content
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'on_receive_header');
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'on_receive_content');
//send your other custom headers somewhere like here
if (false === curl_exec($ch)) {
//handle error better than this.
die(curl_error($ch));
}
function on_receive_header($ch, $string) {
//You could here forward the other headers received from your other server if you wanted
//for now we only want Content-Length
if (stripos($string, 'Content-Length') !== false) {
header($string);
}
//curl requires you to return the amount of data received
$length = strlen($string);
return $length;
}
function on_receive_content($ch, $string) {
echo $string;
//again return amount written
$length = strlen($string);
return $length;
}