在 PHP 中获取 URL 的文件名

Ajm*_*lim 1 php

我想从 URL 获取文件名。但问题是它并没有以延期结束。

例如,http://something.com/1245/65/。单击此 URL,我们将获得一个 PDF 文件。如何将该文件的文件名存储在变量中?

Bra*_*tie 5

<?php
  header('Content-Type: text/plain');

  $curl = curl_init('http://localhost/fakefile.php');

  curl_setopt($curl, CURLOPT_HEADER, true);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD');

  if (($response = curl_exec($curl)) !== false)
  {
    if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200')
    {
      var_dump($response);

      $reDispo = '/^Content-Disposition: .*?filename=(?<f>[^\s]+|\x22[^\x22]+\x22)\x3B?.*$/m';
      if (preg_match($reDispo, $response, $mDispo))
      {
        $filename = trim($mDispo['f'],' ";');

        echo "Filename Found: $filename";
      }
    }
  }

  curl_close($curl);
Run Code Online (Sandbox Code Playgroud)

这将解析该Content-Disposition行以获取filename=foo.bar信息(假设它确实使用此方法直接渲染文件。)