0

So I was getting fatal errors when handling remote-site file sizes and was removing pieces of the code to find the culprit, which was get_headers. I then printed its value and it said it was an array rather than a string (or number).

print_r(get_headers("https://www.example.com/folders/file.zip",1)["Content-Length"]);

Array
(
    [0] => 169
    [1] => 17936534
)

Why is this?

Edward
  • 495
  • 1
  • 6
  • 20
  • 1
    Probably a multipart response. Try dumping all the headers and see what you get. – Sammitch Oct 19 '22 at 22:17
  • Topical: [What's the "Content-Length" field in HTTP header?](https://stackoverflow.com/q/2773396/2943403) ...since Stack Overflow's "Related" list is never smart enough to actually include anything remotely "related" to the asked question. – mickmackusa Oct 19 '22 at 22:32
  • @Sammitch So I looked at all parts of `get_header` and the first entry says "301 Moved Permanently". The entries Server, Date, and Connection returned an array with 2 identical entries each, and Content-Type returned an array with "text/html" and "application/zip". – Edward Oct 20 '22 at 04:28
  • 2
    So you actually have two requests there then, the first got answered with a 301 redirect, and therefor your system automatically made a second request for the new URL. The 169 would be the Content-Length of that first redirect response then (it probably came with a short document saying something like "Moved to ...", that would be those 169 bytes), and the 17936534 is the Content-Length of the response body returned by that second URL. – CBroe Oct 20 '22 at 08:51
  • 1
    For an easy workaround, you can assign the value you get to a variable like `$contentLength` first, and then do something like `$contentLength = is_array($contentLength) ? $contentLength[array_key_last($contentLength)] : $contentLength;` (By using `array_key_last` it is not limited to working with two array elements; if you got say three values back, because there were _two_ redirects, it would still get you the last value.) – CBroe Oct 20 '22 at 08:55

0 Answers0