Questions about PHP getting file name extensions

Source: Internet
Author: User
Keywords Php
Tags bz2
In general, get similar test.txt, test.html, test.png and other extensions are fine.

such as Test.png.txt, the problem is not very big, because we can think. png is a file name instead of an extension

But what if it's test.tar.gz? White list?

Reply content:

In general, get similar test.txt, test.html, test.png and other extensions are fine.

such as Test.png.txt, the problem is not very big, because we can think. png is a file name instead of an extension

But what if it's test.tar.gz? White list?

The file name suffix should standardize test.tar.gz. Tar is a packaged file. GZ is a compressed file
Linux compressed files are very canonical
TEST.TAR.GZ gzip Compressed tar packaging
TEST.GZ gzip compressed, not packaged
TEST.TAR.BZ2 BUNZIP2 Compressed Tar Package
TEST.BZ2 BUNZIP2 Compressed not packaged

In the programming world, only the underlying operations can be constrained to achieve graceful upper-level code. If a file is in% as a file suffix
If my interface is designed like this,

class fileInfo(){   public function reFileName(); #文件名   public function getRealSuffix();  #最后一个  test.tar.gz   返回gz   public function getFullSuffix();    #全部后缀test.tar.gz   返回tar.gz   public function getSuffixs();   #后缀名数组}

File formats can be restricted, matched by known suffixes

The extension can be obtained in this way

$info = pathinfo($file);echo $info['extension'];

In fact, you want to do file type filtering, it is recommended to use mimetype instead of extension, more reliable.
Reference http://php.net/manual/zh/book.fileinfo.php can be used directly from the php5.3.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.