0

Im looking at a snippet i found on the internet but im not sure what one part of the script does.

// parse the Content-Disposition header, if available:
$file_name = $this->getServerVar('HTTP_CONTENT_DISPOSITION') ?
rawurldecode(preg_replace('/(^[^"]+")|("$)/', '', $this->getServerVar('HTTP_CONTENT_DISPOSITION'))) : null;


protected function getServerVar($id)
{
    return isset($_SERVER[$id]) ? $_SERVER[$id] : '';
}

I tried to print this "print_r($this->getServerVar('HTTP_CONTENT_RANGE') ?"

But i get nothing.

Could someone provide in depth information on what exactly HTTP_CONTENT_DISPOSITION is and what is happening in the above code

Also does what does HTTP_CONTENT_RANGE mean?

Thanks!!!

GNov
  • 105
  • 1
  • 13
  • Is this inside of a class? I'm assuming since you have a `protected` function and are referencing `$this` but you do not specify anywhere and I don't like to assume (if no class, this would also cause the problems you are having which is why I ask). Try turning on error_reporting and/or checking the error_log because it will likely say exactly what is wrong. – Jonathan Kuhn Jan 29 '15 at 21:10
  • Yes this is in a class. – GNov Jan 29 '15 at 21:11
  • @JonathanKuhn The actually script works fine, i just dont understand what exactly this code does. – GNov Jan 29 '15 at 21:12
  • 1
    content-disposition is a http response header. AFAIK, it is not normally set with the request (which is what php handles). It is normally used to specify whether the body of the response is supposed to be displayed inline or as an attachment (download). You would use it when forcing a download to set the name of the file being downloaded, else the browser would just use the script name as the download name. – Jonathan Kuhn Jan 29 '15 at 21:21
  • I see, but the code is sending the "HTTP_CONTENT_DISPOSITION" to the getServerVar function. – GNov Jan 29 '15 at 21:25
  • @JonathanKuhn If you try try execute "print_r($_FILES['HTTP_CONTENT_DISPOSITION ']);" you get an error saying that Undefined index: HTTP_CONTENT_DISPOSITION – GNov Jan 29 '15 at 21:27
  • Since `HTTP_CONTENT_DISPOSITION` is not part of the `$_FILES` array, I wouldn't expect the index to exist. Your `getServerVar` method is just checking if the key exists in the `$_SERVER` array (which is where all http headers go) and if it exists return that value or else return a blank string. The part of the code that calls that function (the preg_replace) is removing anything from the start of the string up to the first double quote and a trailing quote if `HTTP_CONTENT_DISPOSITION` exists. – Jonathan Kuhn Jan 29 '15 at 23:01
  • Typically all request headers are added to the `$_SERVER` array and preceded with the `HTTP_` prefix. Just by looking at this, I would guess it is part of a RESTful system and that is how files are being requested for download. Just a guess though. – Jonathan Kuhn Jan 29 '15 at 23:03

1 Answers1

0

I would have left this as a comment if I had enough creds. But here is a link you might find helpful

Uses of content-disposition in an HTTP response header

Community
  • 1
  • 1
scallopboat
  • 98
  • 1
  • 2
  • 10
  • Typically one wouldn't reference another SO answer as an answer. That is what is considered a duplicate question. – Mike Brant Jan 29 '15 at 21:24