PHP face test file directory operation _php Instance

Source: Internet
Author: User

This problem is not difficult, the test center is mainly in function substr () STRRCHR () Array_pop () Strrpos () Strpos (), Strrev () explode ().

No more nonsense to say, directly to everyone paste code.

Get file suffix, traverse directory hierarchy

/** * 5 ways to get file suffix name * This question is actually the test function substr () STRRCHR () Array_pop () Strrpos () Strpos () Strrev () explode () pathinfo () * @param St Ring $filename filename to process 1.jpg/1.png/1.html/1.php * @return string. php/php/function Get_ext1 ($filename) {return St
RRCHR ($filename, '. '); function Get_ext2 ($filename) {return substr ($filename, Strrpos ($filename, '. '));} function Get_ext3 ($filename) {R
Eturn Array_pop (Explode ('. ', $filename)); function Get_ext4 ($filename) {return pathinfo ($filename, pathinfo_extension);} function Get_ext5 ($filename) {RET
Urn Strrev (substr (Strrev ($filename), 0,strpos (Strrev ($filename), '. '));
} $filename = "1.php"; echo Get_ext1 ($filename). "
\ n "; echo get_ext2 ($filename). "
\ n "; echo Get_ext3 ($filename). "
\ n "; echo Get_ext4 ($filename). "
\ n "; echo Get_ext5 ($filename). "
\ n "; /** * Traversal directory (Recursive) * @param string $dir directory name and last no/* @return array $files The entire directory structure */function Myscandir ($dir) {$files = arr
    Ay (); if ($handle = Opendir ($dir)) {while ($file = Readdir ($handle)!= false) {if ($file!= "." && $file!= "...") {$newDir = $dir. "
                /". $file;
                if (Is_dir ($newDir)) {$files [$file] = Myscandir ($newDir);
                }else{$files [] = $file;
        }} closedir ($handle);
    return $files;
}} $dir = "/dirname"; Print_r (Myscandir ($dir));

The above code is not very simple ah, on these several functions, the function mastered, this problem is very easy to solve, I hope this article to share for everyone to help.

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.