Hello! Using curl, I get a header file that lies along this path http://zakupki.gov.ru/223/purchase/public/download/download.html?id=3324768 . Now I need to get from the header the file name or file extension. Header has this form

HTTP/1.1 200 OK Server: nginx Date: Sun, 24 May 2015 12:42:09 GMT Content-Type: application/download Content-Length: 648192 Connection: close Set-Cookie: route223p7=0; Domain=zakupki.gov.ru; Path=/223 Content-Disposition: attachment; filename="%D0%9A%D0%BE%D0%BD%D0%BA%D1%83%D1%80%D1%81%D0%BD%D0%B0%D1%8F%20%D0%B4%D0%BE%D0%BA%D1%83%D0%BC%D0%B5%D0%BD%D1%82%D0%B0%D1%86%D0%B8%D1%8F%2070-%D1%8D%D1%82%D0%B7%D0%BF.doc"; filename*=UTF-8''%D0%9A%D0%BE%D0%BD%D0%BA%D1%83%D1%80%D1%81%D0%BD%D0%B0%D1%8F%20%D0%B4%D0%BE%D0%BA%D1%83%D0%BC%D0%B5%D0%BD%D1%82%D0%B0%D1%86%D0%B8%D1%8F%2070-%D1%8D%D1%82%D0%B7%D0%BF.doc Content-Language: en-US Expires: Sun, 24 May 2015 12:57:09 GMT Cache-Control: max-age=900 Cache-Control: public 

How do I get the value of filename, or .doc. Thank you in advance.

  • According to your link 404. - user6550
  • Pull content-disposition from heading - etki
  • When you click on the link, the file starts downloading. - Hamo
  • How to get out of the content-disposition header? - Hamo
  • one
    In general, the native get_headers function has long been available for parsing received headers. But if you want to design bikes, then go ahead. - u_mulder

2 answers 2

Tip @Deadooshka helped me a lot, thanks to him.

 <?php $ch=curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_NOBODY, 1); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0'); $header = curl_exec($ch); if(preg_match('~Content-Disposition\:\h*attachment\;\h*filename=\"\K[^\"]+~i',$header,$match)){ $fileName = urldecode($match[0]); echo $fileName; } curl_close ($ch); ?> 

The code works fine.

    $ curl_output; // Received string from B-urla

    $ result = array (); // Array of the results found

    preg_match ('/ filename = "([ABCDEFabcdef0-9 \% -] {0,}. [a-zA-Z0-9] {0,})" /', $ curl_output, $ result);

    $ result [1]; // file name with extension

    • preg_match is a function that searches for substrings using regular expressions - ActivX