开发者

php obtaining complex file extension like mysql-5.0.1.tar.gz

I've run into a typical problem here. Till now i was doing

strstr($filename,".");

$filename is the file i got from $_FILE i.e up开发者_Go百科loaded file.It was running fine until i hit a filename of the type i mentioned. Even doing

pathinfo($filename);

gives me

.gz 

I need to see whether it is EXACTLY

.tar.gz


Technically, pathinfo is correct: the one and only extension for that file is .gz. The fact that it has .tar in the name is as coincidental as the fact that it has 5.0.1 in it.

That doesn't make your interest in checking for .tar.gz files invalid, but it does raise the question: what specifically do you want to find?

The most direct solution to your specific question is: first look for the extension (via pathinfo or the strpos function) and then if it happens to be .gz look for the "extension" in the remaining filename (via the same technique).

$parts = pathinfo($filename);
$extension = $parts['extension'];
if ($extension == '.gz') {
    $parts = pathinfo($parts['filename']);
    $extension = $parts['extension'] . $extension;
}


The most simple way would be to check for the last 7 characters of the filename - this ensures that every file ends with .tar.gz:

if (substr($filename, -7) == '.tar.gz') {
    // continue
}


And if you needed to parse 'this.is.a.very.long.file.name.with.lots.of.full.stops' then what part of that is the file extension? Relying on a particular part of a filename to convey semantic, machine readable information about the contents of the file is, at best dangerous.

It's not clear what the problem you are trying is - why do you need to know what the extension is?

C.


I just posted the following enhanced version of pathinfo() in comments over on PHP.net. This version groups all of the parts of the file extension in as the extension (e.g. "tar.gz" instead of just "gz"), leaving the rest as the filename:

<?php
  function pathinfo_enhanced($file_path) {
    $core_path_info = pathinfo($file_path);
    $filename = $core_path_info['filename'];

    if (isset($core_path_info['extension'])) {
      $extension = $core_path_info['extension'];
    } else {
      $extension = '';
    }

    $extension_parts = array();

    while (!empty($extension)) {
      array_unshift($extension_parts, $extension);

      $remaining_path_info = pathinfo($filename);
      $filename = $remaining_path_info['filename'];

      if (isset($remaining_path_info['extension'])) {
        $extension = $remaining_path_info['extension'];
      } else {
        $extension = '';
      }
    }

    $revised_path_info = array(
      'filename'  => $filename,
      'extension' => implode('.', $extension_parts),
    );

    return array_merge($core_path_info, $revised_path_info);
  }

Here are some examples you can run to show how it handles the different cases:

  // Directory; two extensions
  $path = '/www/htdocs/inc/file.tar.gz';
  $info = pathinfo_enhanced($path);

  echo "$path\n";
  print_r($info);
  echo "\n";

  // Directory; one extension
  $path = '/www/htdocs/inc/file.tgz';
  $info = pathinfo_enhanced($path);

  echo "$path\n";
  print_r($info);
  echo "\n";

  // Directory; no extension
  $path = '/www/htdocs/inc/lib';
  $info = pathinfo_enhanced($path);

  echo "$path\n";
  print_r($info);
  echo "\n";

  // No directory; one extension
  $path = 'test.php';
  $info = pathinfo_enhanced($path);

  echo "$path\n";
  print_r($info);
  echo "\n";

  // No directory; dot file
  $path = '.example';
  $info = pathinfo_enhanced($path);

  echo "$path\n";
  print_r($info);
  echo "\n";

  // Directory only
  $path = '/www/htdocs/inc/';
  $info = pathinfo_enhanced($path);

  echo "$path\n";
  print_r($info);
  echo "\n";


THIS is the answer:

strstr(pathinfo('asdasd/qweqwe/asdasd.tar.gz')['basename'], '.');

will return you '.tar.gz'

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜